關(guān)于proteus仿真的串口問題
以下四幅圖都是關(guān)于串口中斷的問題,串口中斷需要一個(gè)接收或者發(fā)送數(shù)據(jù)的觸發(fā)。
圖一:因?yàn)橛纱谛≈职l(fā)送的數(shù)據(jù)達(dá)到了單片機(jī)串口,所以引起了串口的中斷。
圖二:圖一的大圖。
圖三:因?yàn)橛纱谛≈职l(fā)送的數(shù)據(jù)達(dá)到了virtual terminal,沒有到達(dá)串口,所以沒有引起串口的中斷。
圖四:圖一和圖三的綜合,也不行
main.c
#include "os_cfg.h"
#include "task0.h"
#include "task1.h"
#include "task2.h"
#include "task3.h"
void (* code task[])() = {task0,task1,task2,task3};
void main(void)
{
uchar i;
os_timer0_init();
os_timer1_init();
EA = 1;//開總中斷
while(1)
{
for(i=0;i
}
}
os_cfg.h
#include "reg52.h"
#include "macroandconst.h"
#define TIME_PER_SEC 200 //定義任務(wù)時(shí)鐘頻率,200Hz
#define CLOCK 22118400//定義時(shí)鐘晶振,單位Hz
#define MAX_TASK 4//定義任務(wù)數(shù)量
extern unsigned char task_delay[MAX_TASK];
extern void run(void (*ptask)());
extern void os_timer0_init(void);
extern void os_timer1_init(void);
macroandconst.h
#ifndef _MACRO_AND_CONST_H_
#define _MACRO_AND_CONST_H_
typedef unsigned int uint16;
typedef unsigned int UINT;
typedef unsigned int uint;
typedef unsigned int UINT16;
typedef unsigned int WORD;
typedef unsigned int word;
typedef int int16;
typedef int INT16;
typedef unsigned long uint32;
typedef unsigned long UINT32;
typedef unsigned long DWORD;
typedef unsigned long dword;
typedef long int32;
typedef long INT32;
typedef signed char int8;
typedef signed char INT8;
typedef unsigned char byte;
typedef unsigned char BYTE;
typedef unsigned char uchar;
typedef unsigned char UINT8;
typedef unsigned char uint8;
typedef unsigned char BOOL;
#endif
task0.h
#ifndef _TASK0_H_
#define _TASK0_H_
extern void task0(unsigned int db);
#endif
task1.h
#ifndef _TASK1_H_
#define _TASK1_H_
#define ADC0804_DB P1
extern void task1(void);
#endif
task2.h
#ifndef _TASK2_H_
#define _TASK2_H_
extern void task2(void);
#endif
task3.h
#ifndef _TASK3_H_
#define _TASK3_H_
extern void task3(void);
#endif
os_c.c
#include "os_cfg.h"
#include "task1.h"
uchar task_delay[MAX_TASK];
uchar data_buffer;
//定時(shí)器0初始化
void os_timer0_init(void)
{
uchar i;
for(i=0;i
TH0 = (65536-CLOCK/TIME_PER_SEC/12) / 256;//CRY_OSC,TIME_PER_SEC在easycfg.h中配置
TL0 = (65536-CLOCK/TIME_PER_SEC/12) % 256;
TR0 =1;
ET0 =1;//開啟定時(shí)器和中斷
}
//定時(shí)器1初始化
void os_timer1_init(void)
{
SCON = 0x50;//串行口工作方式1,REN=1允許接受串行數(shù)據(jù)
PCON = 0; //電源控制寄存器 SMOD=0,波特率保持不變
TMOD = 0x20; //定時(shí)器T1初始化,工作方式2
TH1 = 0xFD;//產(chǎn)生波特率為9600bit/s的計(jì)數(shù)初值
TL1 = 0xFD;
TR1 = 1;
ES = 1;//允許串口中斷
}
// 系統(tǒng)OS定時(shí)中斷服務(wù)
void os_timer0(void) interrupt 1
{
uchar i;
TH0 = (65536-CLOCK/TIME_PER_SEC/12) / 256;//CRY_OSC,TIME_PER_SEC在easycfg.h中配置
TL0 = (65536-CLOCK/TIME_PER_SEC/12) % 256;
for(i=0;i
void os_timer1(void) interrupt 4
{
SBUF = ADC0804_DB;
while(!TI); //等待發(fā)送完成
TI = 0; //清零發(fā)送標(biāo)志位
if(RI)//RI接收中斷標(biāo)志位
{
RI=0;
SBUF = data_buffer;//發(fā)送數(shù)據(jù)
data_buffer = SBUF;//讀取數(shù)據(jù)
}
//if(TI) //TI發(fā)送中斷標(biāo)志位
//{
//TI=0;
//}
}
void run(void (*ptask)())
{
(*ptask)();
}
task0.c
#include "os_cfg.h"
#include "delay.h"
#define TASK_DELAY0 TIME_PER_SEC/40 //數(shù)碼管頻度為40Hz
/*---------共陰極0~f數(shù)碼管編碼 ----------*/
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f};
void task0(uint db)
{
/*為什么位選放在段選前面就不行了呢?*/
uchar bw,sw,gw;//bw,sw,gw分別等于db百位,十位,個(gè)位上的數(shù)
db=db*100/50;
bw=db/100;
sw=db%100/10;
gw=db%10;
P2=0xfe; //點(diǎn)亮第一只數(shù)碼管
P0=table[bw]|0x80; //最高位置0,點(diǎn)亮第一只數(shù)碼管的小數(shù)點(diǎn),
delay(1);
P2=0xfd; //點(diǎn)亮第二只數(shù)碼管
P0=table[sw];
delay(1);
P2=0xfb; //點(diǎn)亮第三只數(shù)碼管
P0=table[gw];
delay(1);
P2=0xf7; //點(diǎn)亮第四只數(shù)碼管
P0=table[0]; //第四只數(shù)碼管一直顯示0
delay(1);
task_delay[0] = TASK_DELAY0;
}
task1.c
#include "os_cfg.h"
#include "delay.h"
#include "intrins.h"
#include "task0.h"
#include "task1.h"
//static uchar i;
sbit wr=P3^6;
sbit rd=P3^7;
#define TASK_DELAY1 TIME_PER_SEC/41 //任務(wù)執(zhí)行頻度為60Hz
void task1(void)
{
wr=0;//在片選信號(hào)CS為低電平情況下(由于CS接地,所以始終為低電平),
_nop_(); //WR由低電平到高電平時(shí),即上升沿時(shí),AD開始采樣轉(zhuǎn)換
wr=1;
delay(1); //延時(shí)1ms,等待采樣轉(zhuǎn)換結(jié)束,延時(shí)函數(shù) delay(1)延時(shí)0.992ms,大約為1ms
//P1=0xff; //這條語句不能少,我也還不知道為什么
rd=0;//將RD腳置低電平后,再延時(shí)大于135ns左右(這里延時(shí)1us),
_nop_(); //即可從DB腳讀出有效的采樣結(jié)果,傳送到P1口
//for(i=0;i<10;i++) //刷新顯示一段時(shí)間
task0(ADC0804_DB); //顯示從ADC0804的DB得到的數(shù)字量
task_delay[1] = TASK_DELAY1;
}
task2.c
#include "os_cfg.h"
#define TASK_DELAY2 TIME_PER_SEC/10 //任務(wù)執(zhí)行頻度為10Hz
void task2(void)
{
// SBUF = dat; //待發(fā)送的數(shù)據(jù)寫入緩沖區(qū)
//while(!TI); //等待發(fā)送完成
//TI = 0; //清零發(fā)送標(biāo)志位
task_delay[2] = TASK_DELAY2;
}
task3.c
#include "os_cfg.h"
#define TASK_DELAY3 TIME_PER_SEC/20 //任務(wù)執(zhí)行頻度為20Hz
void task3(void)
{
task_delay[3] = TASK_DELAY3;
}
delay.h
#ifndef _DELAY_H_
#define _DELAY_H_
extern void delay(unsigned int x);
#endif
delay.c
#include
#include "macroandconst.h"
static uchar i;
void delay(uint x) //延時(shí)函數(shù) delay(1)延時(shí)0.992ms,大約為1ms
{
while(x--)
for(i=0;i<120;i++);
}
程序的目的是:通過ADC0804采集數(shù)據(jù),通過數(shù)碼管顯示,并上傳到上位機(jī),這是程序的初稿,有待完善,歡迎提意見。
附上仿真圖: