关于PIC16F877A串口发送任意字符串的问题
LED2=0)
#define LED3_OPEN() (LED3=1)
#define LED3_CLOSE() (LED3=0)
#defineCPU_FOSC8000000
#defineBAUD9600
unsigned char* LYMS="AT+CWMODE=3\r";
unsigned char* SZLY="AT+CWSAP=\"ESP8277_TYC\"\,\"1234567890\"\,11\,3\r";
unsigned char* RST="AT+RST\r";
unsigned char* LCAP="AT+CWLAP\r";
unsigned char* SZDLJ="AT+CIPMUX=1\r";
unsigned char* KQFU="AT+CIPSERVER=1\,5000\r";
unsigned char* FSSJ="AT+CIFSR\r";
void delay2ms(uint x)
{
uint a,b;
for(a=x; a》0; a--)
for(b=152; b》0; b--);
}
void sys_init()
{
TRISD=0x00;//设置PORTD口为输出
}
void usart_init()
{
TRISC=0x80;//设置RX为输入,TX为输出
TXSTA=0x24;
RCSTA=0x90;
SPBRG=0x33;//本人使用的是8MHz晶振,9600=8000000/(16*(X+1))-》X=51-》X=0x33
RCIE=0x01;
TXEN=0x01;
TXIE=0x01;
PEIE=0x01; //外部中断使能
GIE=1;//总中断使能
}
void usart0_putchar(unsigned char data)
{
while (!TRMT);
TXREG = data;
}
void print(unsigned char* p_string)
{
while (*p_string)
{
if (*p_string != ‘\r’)
{
usart0_putchar(*p_string);
}
else
{
usart0_putchar(‘\r’);
usart0_putchar(‘\n’);
}
p_string++;
}
}
void send_command()
{
print(LYMS);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
print(SZLY);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
print(RST);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
print(LCAP);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
print(SZDLJ);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
print(KQFU);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
print(FSSJ);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
}
void interrupt usart(void)
{
if(RCIE&&RCIF)
{
//TXREG = RCREG;
RX_Buffer[RX_CNT] = RCREG;
if(RX_Buffer[0]==0x45)
RX_CNT++;
else
RX_CNT=0;
if(RX_CNT》=10)
{
if(RX_Buffer[0]==0x45&&RX_Buffer[1]==0x53&&RX_Buffer[2]==0x50)
{
if(RX_Buffer[4]==0x4C&&RX_Buffer[5]==0x45&&RX_Buffer[6]==0x44)
{
if(RX_Buffer[7]==0x31)
{
if(RX_Buffer[3]==0x4B)
LED1_OPEN();
if(RX_Buffer[3]==0x47)
LED1_CLOSE();
}
}
}
RX_CNT=0;
}
}
}
void main()
{
sys_init();
usart_init();
send_command();
LED1_OPEN();
LED2_OPEN();
LED3_OPEN();
while(1);
}
PIC16F877A 相关文章:
- LT3751如何使高压电容器充电变得简单(08-12)
- 三路输出LED驱动器可驱动共阳极LED串(08-17)
- 浪涌抑制器IC简化了危险环境中电子设备的本质安全势垒设计(08-19)
- 严酷的汽车环境要求高性能电源转换(08-17)
- 适用于工业能源采集的技术 (08-10)
- 单片式电池充电器简化太阳能供电设计(08-20)