先上代码,有空在空说明。
main.c
#include
#define W_SIG 0x01
#define W_TMO 0x02
#define W_IVL 0x80
#define SIG_EVENT 0x04
extern unsigned char Os_create_task (unsigned char task_id);
extern unsigned char os_wait (unsigned char typ,unsigned char ticks);
void start_task(void) _task_ 0
{
init();
os_wait(W_TMO, 200);//延时2秒
Os_create_task(1);
Os_create_task(2);
Os_create_task(3);
while (1)
{
SendString("执行任务0rn");
}
}
void OStask1(void) _task_ 1
{
while (1)
{
SendString("执行任务1rn");
}
}
void OStask2(void) _task_ 2
{
while(1)
{
SendString("执行任务2rn");
RUNLED=~RUNLED;
}
}
void OStask3(void) _task_ 3
{
while (1)
{
SendString("执行任务3rn");
KM=~KM;
if(KM==0)
{
SendString("继电器吸合rn");
}
else SendString("继电器断开rn");
}
}
ahphong.c
#include
#include "intrins.h"
//全局变量定义
#define uchar unsigned char
#define uint unsigned int
sfr AUXR = 0x8e;
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 11059200L //System frequency
#define BAUD 115200 //UART baudrate
#define NONE_PARITY 0 //None parity
#define ODD_PARITY 1 //Odd parity
#define EVEN_PARITY 2 //Even parity
#define MARK_PARITY 3 //Mark parity
#define SPACE_PARITY 4 //Space parity
#define PARITYBIT NONE_PARITY //Testing even parity
bit busy;
void init()
{
#if (PARITYBIT == NONE_PARITY)
SCON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
SCON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
SCON = 0xd2; //9-bit variable UART, parity bit initial to 0
#endif
TL2 = RCAP2L = (65536-(FOSC/32/BAUD)); //Set auto-reload vaule
TH2 = RCAP2H = (65536-(FOSC/32/BAUD)) >> 8;
T2CON = 0x34; //Timer2 start run
ES = 1; //Enable UART interrupt
EA = 1; //Open master interrupt switch
}
void SendData(BYTE dat)
{
while (busy); //Wait for the completion of the previous data is sent
ACC = dat; //Calculate the even parity bit P (PSW.0)
if (P) //Set the parity bit according to P
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 0; //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 1; //Set parity bit to 1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 1; //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 0; //Set parity bit to 0
#endif
}
busy = 1;
SBUF = ACC; //Send data to UART buffer
}
void SendString(char *s)
{
while (*s) //Check the end of the string
{
SendData(*s++); //Send current char and increment string ptr
}
}
//串口接收数据处理
void Uart_Isr() interrupt 4
{
if (RI)
{
RI = 0;
}
if (TI)
{
TI = 0; //Clear transmit interrupt flag
busy = 0; //Clear transmit busy flag
}
}
ahphong.h
#ifndef __AHPHONG_H__ #define __AHPHONG_H__ #include#ifndef RAND_MAX #define RAND_MAX 32767u #endif #define true 1; #define false 0; sbit RUNLED = P1^6;//RUN_LED输出脚 sbit KM = P2^4; //继电器输出脚 //全局变量定义 extern void init(void); void SendString(char *s); #endif



