⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usart_2.c

📁 用PROTEUS做的USART仿真
💻 C
字号:
/**********************************
!!!!!!注意在发送的字符串后边需加'/'!!!!!!!!!
可以用‘*’进行清屏
**********************************/
#include <pic.h>

#define uint unsigned int
#define uchar unsigned char
//**********函数声明*************
void  Init_LCD(void);
void LCD_Clear(void);
void delay_nms(unsigned int); 
 void LCD_DisplayString (char, char ,unsigned char*);
void LCD_DisplayCharacter(char);
void LCD_WriteControl(uchar);

//void get_usart_result(void);
void get_usart_string(void);   //receive the string from the USART
void send_usart_date(uchar);
uchar get_usart_date(void);

uchar string_usart1[]="receive...";
uchar string_usart2[]="transmit...";
uchar pc_link_ready=0;
//*************************************
void usartint()
{TRISC6=0;  //TX引脚输出
 TRISC7=1;  //RX引脚输入
 SPBRG=0X67;  //波特率设为,0x67-2400,0x33-4800,0x19-9600
 TXSTA=0X24;  //设为高速波特率,发送8位数据
 RCSTA=0X90;  //串行端口使能
 GIE=0; 
 TRISD=0x00;  
 PORTD=0x00;
 //TXREG=0x88; 
}
/*
//接收USART数据
void get_usart_string()
{
 uint count=0;
 do{
    do{;}while(RCIF==0);
    RCIF=0;
    string_usart[count]=RCREG;
    count++;
    if('*'==RCREG)
    {
     LCD_Clear();   //If send '*',clear the LCD
     break;   // tip the cycle!
    }
    }while(RCREG!='/');    //!!!!!!注意在发送的字符串后边需加'/'!!!!!!!!!
    string_usart[count-1]='\0'; //加停止符
 }
*/

//************获取PC传输的数据*********************
uchar get_usart_date()
{
 uchar usart_date_temp;
 do{;}while(RCIF==0);
 RCIF=0;
 usart_date_temp=RCREG;
 return usart_date_temp;
}
//*********************************
//通过USART向PC发送数据
void send_usart_date(uchar send_temp)
{
 TXREG=send_temp;
 do{;}while(0==TXIF);
 TXIF=0;
}
//***********通过USART发送字符串*****************
void send_pc_string(const uchar *string_temp)
{
 while('\0'!=*string_temp)
 {
  send_usart_date(*string_temp);
  string_temp++;
 }
}
//****************************
void pc_read()
{
 //uchar channel_pc;  //定义PC可设置的通道变量 
 //uchar channel_i;
 
 if(get_usart_date()=='*')
 {
  pc_link_ready=1;
  send_pc_string("link..."); //PC与单片机建立连接
  }
 if(get_usart_date()=='/')
 {
  pc_link_ready=0;  //PC与单片机建立连接
  send_pc_string("unlink...");
 }
// if(1==pc_link_ready)
// send_pc_string("link...");
// else
// send_pc_string("unlink...");
 /*
 if(1==pc_link_ready)  //在建立连接的情况下
 {
  if(6==get_usart_date()) //发送6是读取5路的电压
  {
   for(channel_i=1;channel_i<=5;channel_i++)
   {
    send_usart_date(((uchar)(get_date(channel_i)/100))+0x30);
    send_usart_date('.');
    send_usart_date((get_date(channel_i)%100)/10+0x30);
    send_usart_date((get_date(channel_i)%100)%10+0x30);
    send_usart_date('V');
    send_usart_date(' ');
   }
  }
 }
*/
}
void main()
{
usartint();
//Init_LCD();
//LCD_WriteControl(0x80); //显示起始地址
while(1)
{
pc_read();
// if('T'==get_usart_date())  //发送
// LCD_DisplayString (1,1 ,string_usart2);
// if('R'==get_usart_date())  //接收
// LCD_DisplayString (1,1 ,string_usart1);
 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -