• slider image 514
  • slider image 516
  • slider image 517
  • slider image 518
  • slider image 519
:::


Browsing this Thread:   2 Anonymous Users






problem with usart on pic16F690
#1
新會員
新會員


查看用戶資訊
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
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... ]

教育訓練中心

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