/*
This is a test program about of the PIC16F690 demo board.
//程序内容:
//1.普通I/O驱动
//2.异步串口驱动
//3.内部比较器1驱动
//4.内部数据EEPROM驱动
*/
#include <pic.h>
//系统配置(只读位 编程时烧写这些位)
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT /
& UNPROTECT & BORDIS & IESODIS & FCMDIS);
//*****************************************************************
//*****************************************************************
#define delay_time 250 //delay gene
void test_led(void); //I/O test
void delay(int K); //delay
void interrupt ISrR(void); //interrupt
int main(void); //main
void uart_send_char(unsigned char a);
void uart_send_string(unsigned char *string);
void write_eeprom(int data);
unsigned char volatile rc_char=0x00;
void delay(int K) //Simple Delay Loop
{
char i,j;
for (i = 0; i < 250; i++)
for (j = 0; j < K; j++);
}
void test_led(void)
{
PORTC=0x00;
delay(delay_time);
PORTC=0x0F;
delay(delay_time);
}
//-----------------------------------------------------------------------------
int main(void)
{
unsigned char txd_char=0x07;
// unsigned char volatile rc_char=0x00;
//*****************************************************************************
//system initialize
TRISC = 0x00;
PORTC = 0x00;
ANSEL = 0x00;
ANSELH = 0x00;
SPBRG = 25; //波特率设为9600
TXSTA = 0x24; //使能发送
RCSTA = 0x90; //使能接收
RCIF = 0; //清零外部中断
GIE = 1; //开放全局中断
PEIE = 1; //使能外设中断
TRISC = 0x00; //将PORC定为输出
TRISB = 0x20; //将PORB RXD 设为输入口,TXD设为输出口
RCIE=1; //使能接收中断
//*****************************************************************************
//RA0为模拟信号比较器的同相输入端
TRISA= TRISA|0x01; //端口A第0位RA0输入状态(数字输出驱动器为高阻态)
ANSEL= ANSEL|0x01; //端口A第0位RA0输入状态(数字输入缓冲器为高阻态)
//RA1为模拟信号比较器的反相输入端
TRISA = TRISA|(0x01<<1); //端口A第1位RA1输入状态(数字输出驱动器为高阻态)
ANSEL = ANSEL|(0x01<<1); //端口A第1位RA1输入状态(数字输入缓冲器为高阻态)
//RA2为模拟信号比较器1在芯片内部输出后,引出到外部引脚的输出
TRISA = TRISA&(~(0x01<<2)); //端口A第2位RA2为数字输出状态
CM1CON0= CM1CON0|(0x01<<7); //比较器C1比较使能(C1ON)
CM1CON0= CM1CON0|(0x01<<5); //比较器C1输出使能(C1OE)
CM1CON0= CM1CON0&(~(0x01<<2)); //比较器C1同相输入端选择外部引脚RA0输入(C1R)
CM1CON0= CM1CON0&(~0x03); //比较器C1反相输入端选择外部引脚RA1输入(C1CH)
//*****************************************************************************
//do any thing.
for(;;){
PORTC=0x00;
delay(delay_time);
PORTC=0x0F;
delay(delay_time);
//test_led();
// uart_send_char('A');
// uart_send_char('/n');
//uart_send_char('/t');
// uart_send_string("now test uart send a string!!! /n ");
for(unsigned char i=0;i<100;i++)
{
// EEADR=i; //寻址地址
// EEPGD=0x00; //操作数据EEPROM区
// RD=0x01; //启动读操作
//EEDAT=0x00;
//uart_send_char(EEDAT);
EEADR=i; //寻址地址
EEPGD=0x00; //操作数据EEPROM区
RD=0x01; //启动读操作
// EEDAT=0x00;
uart_send_char(EEDAT);
}
//for(unsigned char i=0;i<100;i++) write_eeprom(i);
//while(!TRMT); //等待接收中断
}
//*****************************************************************************
return 0;
}
//-----------------------------------------------------------------------------
//似乎所有的中断都要从这里开始.
//********************************************?
void interrupt ISrR(void)
{
if(RCIF){ //判断是否为串口接收中断
RCIF=0;
PORTC=0x0C;
if(OERR){
CREN=0;
CREN=1;
}
}
rc_char=RCREG;
TXREG=rc_char;
}
//-----------------------------------------------------------------------------
//串口发送一个字符
void uart_send_char(unsigned char a)
{
TXREG=a;
while(!TRMT);
}
//-----------------------------------------------------------------------------
//串口发送一个字符串
void uart_send_string(unsigned char *string)
{
while(*string){
TXREG=*string++;
while(!TRMT);
}
}
//-----------------------------------------------------------------------------
void write_eeprom(int data)
{
EEADR=data;
EEDAT=data; //欲写入之数据
EEPGD=0x00; //操作数据EEPROM区
WREN=0x01; //使能写操作
GIE=0x00; //禁止中断
EECON2=0x55;
EECON2=0xAA;
WR=0x01;
GIE=0x01;
delay(0x02);
WR=0x00;
}