Browsing this Thread:
1 Anonymous Users
Topic options
View mode
舊的在前
problem with usart on pic16F690
新會員
Joined
: 2009/3/24 12:50最後登入時間
: 2010/2/26 12:21
Group:
註冊會員
Level : 1
HP : 0 / 0
MP : 0 / 30
EXP : 2
Greeting. I have a problem with usart on pic16f690. The compiler is HiTech C. I enable receive interrupt for the USART, however it doesn’t work. It just only do transmit character b to PC, when I have transmitted character c from PC to MCU. Could someone give me suggestion or point out which routine is wrong ? here is my code -------------------------------------------------- #include < pic16f685.h > #include "usart.h" #include "delay.h" void Init(void); extern unsigned char status_flag@0x26; static bit flag1 @ ((unsigned)&status_flag*8)+0; //***************************************************** //Main() - Main Routine //***************************************************** void main() { Init(); while(1) { DelayMs(200); if(flag1==1) { putch('a'); GIE=1; } else putch('b'); DelayMs(200); } } void Init() { OPTION =0; TRISC =0; TRISC4 =1; TRISB =0; OSCCON = 0b01100000; OSCTUNE = 0b00000000; ANSEL = 0b00000000; C1ON =0; C2ON =0; CCP1CON =0; ADCON1 = 0B01010000; ADCON0 =0; GODONE =0; TMR1CS =0; // T1CKPS1 =0; // T1CKPS0 =0; // TRISB5 =1; TXSTA=0b00100000; RCSTA=0b10010000; SCKP=0;// SPBRG=25; SYNC =0; BRGH =1; BRG16 =0; INTCON=0; PIE1=0; GIE=1; PEIE=1; TXIF=0; TXIE=0; RCIF=0; RCIE=1; CCP1IE=0; TMR2IE=0; TMR1IE=0; flag1=0; } //********************************************** //interrupt routine //********************************************** static void interrupt isr(void) // interrupt function - { unsigned char abc; GIE=0; if(RCIF==1&&RCIE==1) { abc=RCREG; if(abc=='c') { flag1=1; } else flag1=0; } } ------------------------------------------------ usart.h code #ifndef _SERIAL_H_ #define _SERIAL_H_ #define BAUD 9600 #define FOSC 4000000L #define NINE 0 /* Use 9bit communication? FALSE=8bit */ #define DIVIDER ((int)(FOSC/(16UL * BAUD) -1)) #define HIGH_SPEED 1 #if NINE == 1 #define NINE_BITS 0x40 #else #define NINE_BITS 0 #endif #if HIGH_SPEED == 1 #define SPEED 0x4 #else #define SPEED 0 #endif #if defined(_16F87) || defined(_16F88) #define RX_PIN TRISB2 #define TX_PIN TRISB5 #else #define RX_PIN TRISC7 #define TX_PIN TRISC6 #endif /* Serial initialization */ #define init_comms()\ RX_PIN = 1; \ TX_PIN = 1; \ SPBRG = DIVIDER; \ RCSTA = (NINE_BITS|0x90); \ TXSTA = (SPEED|NINE_BITS|0x20) void putch(unsigned char); unsigned char getch(void); unsigned char getche(void); #endif -------------------------------- usart.c #include < pic.h > #include < stdio.h > #include "usart.h" void putch(unsigned char byte) { /* output one byte */ while(!TXIF) /* set when register is empty */ continue; TXREG = byte; } unsigned char getch() { /* retrieve one byte */ while(!RCIF) /* set when register is not empty */ continue; RCIF=0; return RCREG; } unsigned char getche(void) { unsigned char c; putch(c = getch()); return c; }
發表於: 2009/3/24 12:59
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.