本帖最后由 max0147 于 2012-5-29 11:23 编辑
INTEL 8253,可以正常写入控制字,并输出正确波形;想要读出计数值,但一写入锁存命令,即RL0,RL1都=0时,正常波形也没有了,读出数值始终为0;
调整时序;在读之前加入控制判断GATE都无效,遂请教,多谢。
#include <reg52.h> //包括一个52标准内核的头文件
#include <intrins.h>
#include <stdio.h>
sbit A0 = P0^0;
sbit A1 = P0^1;
sbit nWR = P0^2;
sbit nRD = P0^3;
sbit nCS = P0^4;
sbit gate = P2^4;
void init_82c54(void)
{
A0 = 1;
A1 = 1;
nWR = 1;
nRD = 1;
nCS = 1;
gate = 1;
}
void write_cntl_82c54(unsigned char cnt_no,unsigned char mode)
{
unsigned char cntl_byte;
cntl_byte = 0;
A0 = 1;
A1 = 1;
//SC位
if(cnt_no==0) {cntl_byte &= 0x3f;}
else if(cnt_no==1) {cntl_byte &= 0x3f; cntl_byte |= 0x40;}
else if(cnt_no==2) {cntl_byte &= 0x3f; cntl_byte |= 0x80;}
//RW位
cntl_byte &= 0xcf;
cntl_byte |= 0x10;
//MODE位
if(mode==2) {cntl_byte &= 0xf9; cntl_byte |= 0x04;}
else if(mode==3) {cntl_byte &= 0xf9; cntl_byte |= 0x06;}
//BCD位
cntl_byte &= 0xfe;
//写入
P1 = cntl_byte;
nCS = 0;
nWR = 0;
_nop_();
nWR = 1;
nCS = 1;
}
void write_data_82c54(unsigned char cnt_no,unsigned char indata)
{
//选择counter
if(cnt_no==0) {A0 = 0; A1 = 0;}
else if(cnt_no==1) {A0 = 1; A1 = 0;}
else if(cnt_no==2) {A0 = 0; A1 = 1;}
//写入
P1 = indata;
nCS = 0;
nWR = 0;
_nop_();
nWR = 1;
nCS = 1;
A0 = 1;
A1 = 1;
}
unsigned char read_data_82c54(unsigned char cnt_no)
{
unsigned char cntl_byte;
unsigned char read_byte;
cntl_byte = 0;
A0 = 1;
A1 = 1;
//gate = 0;
//counter latch模式
//SC位
if(cnt_no==0) {cntl_byte &= 0x3f;}
else if(cnt_no==1) {cntl_byte &= 0x3f; cntl_byte |= 0x40;}
else if(cnt_no==2) {cntl_byte &= 0x3f; cntl_byte |= 0x80;}
//D5,D4位
cntl_byte &= 0xcf;
//写入
P1 = cntl_byte;
nCS = 0;
nWR = 0;
_nop_();
nWR = 1;
nCS = 1;
此处写锁在命令,一执行后,正常输出波形就没有了。
//while(1);
//读总线
_nop_();
_nop_();
//选择counter
if(cnt_no==0) {A0 = 0; A1 = 0;}
else if(cnt_no==1) {A0 = 1; A1 = 0;}
else if(cnt_no==2) {A0 = 0; A1 = 1;}
nCS = 0;
nRD = 0;
_nop_();
_nop_();
//读出
read_byte = P1;
_nop_();
nRD = 1;
nCS = 1;
return read_byte;
}
void main(void) // 主程序
{
uchar c1,c2;
//system_init(); //系统初始化
init_82c54();
write_cntl_82c54(0,3);
write_data_82c54(0,0x55);
//write_cntl_82c54(0,3);
//write_data_82c54(0,0x55);
Delay400Ms();
P3 = read_data_82c54(0);
}
8253.JPG (534.42 KB, 下载次数: 1)
下载附件
2012-5-29 10:23 上传
一周热门 更多>