|
#include
__CONFIG(0x09e2); // config1
__CONFIG(0x1fff); // config2.
#define NUM_OF_BUFFER 15 //缓冲区大小 BYTE(s)
unsigned char recv_buffer_[NUM_OF_BUFFER];
unsigned char rx_counter_;
void Initialize()
{
OSCCON = 0x00; //时钟源配置,使用外部晶振 11.0592M
//----------------------
TRISA = 0x3B; //00111011B; //配置端口方向
TRISC = 0x30; //00110000B; //配置端口方向
//初始化端口的值
RA2 = 1;
RC0= 0;
//PORTC = 0x0A;
//_delay(0x20);
//--------------------
TXSTA = 0x00; //发送控制寄存器
RCSTA = 0x90; //接收控制寄存器
BAUDCON = 0x01; //波特率控制寄存器 8位波特发生器
SPBRG = 0x08; //设置波特率为 19200
//------------------------
OPTION_REG = 0xc7; //OPTION寄存器 Timer0 采用256分频
TMR0 = 0x94; //定时器0 初始值,使RA2产生 200 Hz的方波
//------------------------
//INTCON = 0xE0; //中断寄存器
GIE = 1;
PEIE = 1;
TMR0IE = 1;
TMR0CS = 0;
//PIE1 = 0xA1; //外设中断允许寄存器1
RCIE = 1;
}
void interrupt HandleInt()
{
if(TMR0IE && TMR0IF)
{
TMR0IF = 0;
TMR0 = 0x94; //重新设置定时器的值
RA2 = !RA2;
}
else if(RCIE && RCIF)
{
recv_buffer_[rx_counter_++] = RCREG;
if(rx_counter_ == 15)
{
rx_counter_ = 0;
}
}
}
void main()
{
/******* INITALIZE **********/
Initialize();
while(1)
{
;
}
}
[ 本帖最后由 没发动机的摩托 于 2012-10-13 20:43 编辑 ]
|
|