• slider image 442
  • slider image 483
  • slider image 484
  • slider image 485
  • slider image 486
  • slider image 487
  • slider image 488
  • slider image 491
:::


Browsing this Thread:   1 Anonymous Users




« 1 (2)


使用USART接收資料的問題(我成功了^_^)
#1
中級會員
中級會員


查看用戶資訊
Dear All~
小弟想用Timer0計時,並設成高優先中斷。
開啟資料接收中斷的功能,設為低優先中斷。
然後在鍵盤上接下'c'時送出字串。當按下'p'時停止輸出字串。
我是修改曾百由老師的範例,請問一下:

1.就是我是使用APP013,他送給pic的Fose=24MHZ,那我的Timer0的計數,是不是就是使用24MHZ來計算指令週期呢?因為這樣Fosc不是由外部送進來的嗎?所以我指定Timer0使用內部的指命週期,那在計算時Fosc要除以4嗎?
//*******************************************
#include <p18f4550.h> //納入微控制器定義檔
#include <delays.h> //納入時間延遲函式庫定義檔
#include <timers.h>//納入計時器函式庫定義檔
#include <usart.h>//納入計時器USART通訊函式庫定義檔
//Fosc=24MHZ
// 設定微處理器相關結構位元
#pragma config FOSC=HS, BOR=OFF, BORV = 2, PWRT=ON, WDT=OFF, LVP=OFF
#pragma romdata
const rom char LCD_MSG1[]=" C18 Workshop";
const rom char LCD_MSG2[]="Be work Hard";
#pragma romdata

#define count_val 32768
// Timer0 設定為 {1/[(24M/4]/64}*65536s 中斷一次
//Timer0除頻1:64
//----------------------------------------------
//宣告初始程式
void Init_USART(void);
void Init_TMR0(void);

//---------------------------------------------
// 宣告中斷執行程式
void timer0_isr(void);
void RX_isr(void);
//---------------------------------------------
// 狀態旗標變數
struct Flag{
unsigned One_S :1;

unsigned TxD :1;
// UART Tx Continue
} FLAGbits;

//-----------------------------------------------
//進入低優先中斷
#pragma code low_vector=0x018 // 設定中斷進入點
void low_interrupt(void)
{
_asm
goto RX_isr
_endasm
}
#pragma code
//*******************************************
//* Function: isr_high(void) *
//* - Received a serial data from RS-232 *
//* - Save the received data to Rec_Data *
//*******************************************
#pragma interruptlow RX_isr
void RX_isr(void)
{
unsigned char Rec_Data;
PIR1bits.RCIF=0;
Rec_Data=ReadUSART();
if(Rec_Data=='c') FLAGbits.TxD=0;
if(Rec_Data=='p') FLAGbits.TxD=1;
}
//----------------------------------------------
//進入高優先中斷
#pragma code high_vector=0x08
void high_interrupt (void)
{
_asm GOTO timer0_isr _endasm
}
#pragma code

#pragma interrupt timer0_isr

void timer0_isr (void)
{
INTCONbits.TMR0IF = 0;
TMR0H=0x00;
TMR0L=0x00;
PORTD=PORTD^0xFF;
FLAGbits.One_S=1;

}
//------------------------------
void main () {

PORTD = 0x0F; // 將PORTD設定點亮LED
TRISD = 0; // 將TRISD設為0,PORTD設定為輸出
Init_USART();
Init_TMR0();
INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;


while(1) {
while(FLAGbits.TxD==1)
{
if(FLAGbits.One_S==1)
{
FLAGbits.One_S=0;
putrsUSART(LCD_MSG1);
while(BusyUSART());
putrsUSART(LCD_MSG2);
}
}
}
}
//--------------------------------------------------------------

void Init_USART(void)
{
TRISCbits.TRISC7=1;
TRISCbits.TRISC6=0;
TXSTA = 0b00100100;
RCSTA = 0b10010000;
SPBRG = 156;
PIE1bits.RCIE=1;//開啟資料接收中斷功能
PIR1bits.RCIF=0;//清除中斷旗標
IPR1bits.RCIP=0;//設為低優先中斷
}

// The Baud Rate is Fosc/(64*(spbrg+1)); for Low Speed
// The Baud Rate is Fosc/(16*(spbrg+1)); for High Speed
// 156 and High is 9600
//--------------------------------------------------------------
void Init_TMR0(void){

INTCONbits.TMR0IE=1;
T0CON=0b10000101;
TMR0H=0x00;
TMR0L=0x00;

INTCONbits.TMR0IF=0;// 清除中斷旗標
INTCON2bits.TMR0IP=1;//設定為高優先中斷
RCONbits.IPEN=1;//開啟中斷優先功能
INTCONbits.GIEL=1;//開啟低優先中斷功能
INTCONbits.GIEH=1;//開啟高優先中斷功能

FLAGbits.TxD=0; //重置狀態旗標
FLAGbits.One_S=0;
}


發表於: 2007/4/28 15:18
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部







You can view topic.
不可以 發起新主題
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[進階搜尋]


:::

Microchip連結

https://www.facebook.com/microchiptechnologytaiwan/
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=13
https://mu.microchip.com/page/tmu
http://elearning.microchip.com.tw/modules/tad_link/index.php?cate_sn=1
https://page.microchip.com/APAC-PrefCenters-TW.html
http://www.microchip.com/
http://www.microchip.com/treelink
http://www.microchipdirect.com/
http://www.microchip.com.cn/newcommunity/index.php?m=Video&a=index&id=103
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=2
http://www.microchip.com.tw/Data_CD/eLearning/index.html
http://www.microchip.com.tw/RTC/RTC_DVD/
https://www.microchip.com/development-tools/
https://www.youtube.com/user/MicrochipTechnology
[ more... ]

教育訓練中心

!開發工具購買
辦法說明 [業界客戶] [教育單位]
----------------------------------
!校園樣品申請
辦法說明 [教師資格] [學生資格]
----------------------------------