本帖最后由 芯圣电子官方QQ 于 2023-7-24 15:34 编辑
以下是代码过程,欢迎指导!!!!!!!!!
//main.c
#define ALLOCATE_EXTERN //预编译(XRAM部分特殊寄存器)
#include <main.h>
extern u8 Rec_buffer;
char Print[]="21IC的网友们,大家好\n";
int main(void)
{
u8 Tx_count;
CLKSWR = 0x51; //设置时钟输出选择以及分频系数
while(CLKCON & 0x20 != 0x20); //等待设置完成
CLKDIV = 0x01;
LED_INIT(); //LED1、LED2 <==> GPIO初始化
UART_INIT();
KEY_INIT(); //KEY1 <==> GPIO初始化
while(1)
{
LED_BLINK;
Delay_Ms(200);
if(Rec_buffer == 0x55)
{
ES1 = 0;
Rec_buffer = 0;
for(Tx_count = 0; Tx_count < sizeof(Print); Tx_count ++)
{
if(Print[Tx_count] != '\0')
SBUF = Print[Tx_count];
else
break;
while(TI == 0);
TI = 0;
}
REN = 1;
ES1 = 1;
}
}
return 0;
}
//main.h
#ifndef __MAIN_H_
#define __MAIN_H_
#include <HC89F003.h>
#include <led.h>
#include <delay.h>
#include <key.h>
#include <uart.h>
#define LED1_ON_LED2_OFF P2_2 = 0;P1_0 = 1;
#define LED1_OFF_LED2_ON P2_2 = 1;P1_0 = 0;
#define LED_BLINK P2_2 = ~P2_2;P1_0 = ~P1_0;
#define DELAY_ENABLE
#endif
//led.c
#include <led.h>
void LED_INIT()
{
P2M1 = P2M1 & 0xF0 | 0x08; //P22 LED1
P1M0 = P1M0 & 0xF0 | 0x08; //P10 LED2
P1_0 = 1;
P2_2 = 1;
}
//led.h
#ifndef __LED_H_
#define __LED_H_
#include <main.h>
extern void LED_INIT();
#endif
//uart.c
#include <uart.h>
u8 Rec_buffer;
void UART_INIT()
{
P2M0 = P2M0&0x0F|0x80; //P21设置为推挽输出
P0M1 = P0M1&0x0F|0x20; //P03设置为上拉输入
TXD_MAP = 0x21; //IO功能映射
RXD_MAP = 0x03;
T4CON = 0x06;
TH4 = 0xFF; //19200:FFCC 38400:FFE6
TL4 = 0xE6;
SCON = 0x10;
SCON2 = 0x03;
ES1 = 1;
EA = 1;
}
void UART1() interrupt UART1_VECTOR
{
if(RI)
{
REN = 0;
Rec_buffer = SBUF;
RI = 0;
}
}
//uart.h
#ifndef __UART_H_
#define __UART_H_
#include <main.h>
extern void UART_INIT();
#endif
//delay.c
#include <delay.h>
#ifdef DELAY_ENABLE
/*
void Delay_Us(u16 Time_count)
{
uint i;
uchar j;
for(i = 0; i < Time_count; i ++)
for(j = 0; j < 16; j ++);
}
*/
void Delay_Ms(u16 Time_count)
{
u16 j;
for(;Time_count > 0;Time_count --)
for(j = 1596;j > 0;j --);
}
#endif
//delay.h
#ifndef __DELAY_H_
#define __DELAY_H_
#include <main.h>
extern void Delay_Ms(u16 Time_count);
extern void Delay_Us(u16 Time_count);
#endif
|