|
main.c
- /*
- 由T0 做定时器 驱动T1 记数
- 重点在于 TMOD 的配置
- */
- #include <reg52.h>
- #include "main.h"
- void main()
- {
- int display_count=0; //显示的记数
- Sys_init(); // 初始化参数
- Set_Time0(1, 3); // 设置定时器
- Set_Counter0(1);//开启记数器
- while (1) {
- if (TF0 == 1)
- {
- TF0 = 0;
-
- if (use_count++ == Need_time)
- {
- //code
- display_count++;
- count_bit=0; //发生一个跳变沿,驱动T1(记录)
- count_bit=1;//发生一个跳变沿,驱动T1(记录)
- Set_Time0(100, 2);
- use_count=0;
- }
-
- }
- display(TL1); //输出计算数量_最大值255 .需要记录更多次数 再改
- //display(display_count);
- }
- }
- /*初始化参数*/
- void Sys_init()
- {
- Machine_Cycle = 1 / Crystal * Clock_Cycle; // 初始化 机器周期
- use_count = 0;
- }
- /*
- 设置定时器
- 参数1数量: 时间数量
- 参数2单位: 1.微秒 2.毫秒 3.秒
- 参数3模式: 0.定时器 1.计数器
- */
- void Set_Time0(Ulong U_time, int mod)
- {
- /*时间转换*/
- if (mod == 3) {
- Need_time_US = U_time * 1000 * 1000;
- } else if (mod == 2) {
-
- Need_time_US = U_time * 1000;
- } else if (mod == 1) {
-
- Need_time_US = U_time;
- }
- /*设置定时器*/
- Need_time = (Ulong)(Need_time_US / Machine_Cycle) / 65535; // 获得溢出总量
- TR0 = 1;
- TMOD |= 0X01;
- TH0 = (Ulong)(65535 - (Ulong)(Need_time_US / Machine_Cycle) % 65535) / 256; //(Need_time_US/Machine_Cycle)%65535 排除溢出位
- TL0 = (Ulong)(65535 - (Ulong)(Need_time_US / Machine_Cycle) % 65535) % 256;
- }
- /*
- 设置记数器
- 参数1记数器开关: 1:开启 0.关闭
- */
- void Set_Counter0(int Counter_switch){
- if (Counter_switch==0)
- {
- TR1 =0;
-
- }else{
- TR1 =1;
- TMOD |=0X50;
- TH1 =0;
- TL1 =0;
- }
-
- }
复制代码
main.h
- #define Ulong unsigned long
- #define Uint unsigned int
- #define Uchar unsigned char
- #define Uint unsigned int
- #define Uchar unsigned char
-
- sbit DU =P2^6;//数码管段选
- sbit WE =P2^7;//位选
- sbit count_bit =P3^5;
- double code Crystal = 11.0592; // 晶振频率 设定
- double code Clock_Cycle = 12; // 时钟周期
- double Machine_Cycle; // 机器周期
- Ulong Need_time_US; // 微秒总数量
- Ulong Need_time_MS; // 计算溢出的毫秒
- Ulong Need_time_S; // 计算溢出的微秒
- Ulong Need_time; // 溢出的总数量计算
- Ulong use_count; //已经执行的数量
- Uchar code DUtabel[] = {
- 0x3F,
- 0x06,
- 0x5B,
- 0x4F,
- 0x66,
- 0x6D,
- 0x7D,
- 0x07,
- 0x7F,
- 0x6F,
- 0x77,
- 0x7C,
- 0x39,
- 0x5E,
- 0x79,
- 0x71,
- 0x76,
- 0x38,
- 0x40,
- 0x00,
- };
- Uchar code WEtabel[] = {
- 0x00,//全亮,补位选空位
- 0xFE,
- 0xFD,
- 0xfb,
- 0xf7,
- 0xef,
- 0xdf,
- 0xbf,
- 0x7f,
- };
- void Sys_init();
- void Set_Time0(Ulong Us_time, int mod);
- void showval(Uchar val,Uchar WEval);
- void showval(Uchar val, Uchar WEval);
- void delay(Uchar z, int mod);
- void display(Ulong Z);
- void Set_Counter0(int Counter_switch);
- void main1()
- {
- long int a=0;
- while (1) {
- display(333);
- }
- }
- /*
- 数码管显示函数:
- 参数1:显示的数据0-9
- 参数2:需要在显示的位1-8
- */
- void showval(Uchar val, Uchar WEval)
- {
-
-
- WE = 1; // 打开位选锁存
- P0 = WEtabel[WEval]; // 选择位选
- //P0 = 0XFE;
- WE = 0; // 位锁存
-
-
- DU = 1; // 打开段选锁存器
- P0 = DUtabel[val]; // 送数据
- // P0 = 0XFE;
- DU = 0; // 段锁存
-
-
- }
- /*
- 延迟函数.
- 参数1:z为数量
- 参数2:mod为模式
- 1:毫秒
- 2:秒
- */
- void delay(Uchar z, int mod)
- {
- Ulong i;
- if (mod == 1) {
- for (i = 0; i < z; i++) {
- unsigned char a, b;
- for (b = 102; b > 0; b--)
- for (a = 3; a > 0; a--)
- ;
- }
- }
- else if (mod == 2) {
- for (i = 0; i < z * 1000; i++) {
- // 10000us //误差 -0.000000000002us
- unsigned char a, b;
- for (b = 102; b > 0; b--)
- for (a = 3; a > 0; a--) ;
- }
- }
- }
- /*显示长数字*/
- void display(Ulong Z)
- {
- // Z长度
- int Zlen = 0;
- /*存储Z每个位的数字数组变量*/
- int val[8];
- int i;
- while (Z != 0) {
- // 提取n的各个数位上的数
- val[Zlen++] = Z % 10;
- Z /= 10;
- }
- for (i = 0; i < Zlen; i++) {
- P0 = 0XFF; // 清除段码
- showval(val[i], 8 - i);
-
- delay(1, 1);
- }
-
- }
复制代码
|
|