标题:
stc8a单片机怎么在流水灯基础上实现光控,做成光控流水灯
[打印本页]
作者:
duan030
时间:
2021-12-30 11:46
标题:
stc8a单片机怎么在流水灯基础上实现光控,做成光控流水灯
如题 现在已经有了两者结合代码,运用光敏电阻把光照强度串口显示出来了,但是这两者结合却一直没办法实现光控流水灯
代码如下,求大佬指点是哪里错了,应该怎么改正呢
非常感谢!
本次代码中stc8a.h被标注了,但是里面一些必要的定义我直接从中复制到reg52.h里了
单片机源程序如下:
//#include "stc8a.h"
#include "stdio.h"
#include "intrins.h"
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
sbit D0 = P2^0;
sbit D1 = P2^1;
sbit D2 = P2^2;
sbit D3 = P2^3;
sbit D4 = P2^4;
sbit D5 = P2^5;
sbit D6 = P2^6;
sbit D7 = P2^7;
uchar PWM0 = 15; //15 17 19 21 23 25 27 29
uchar PWM1 = 17;
uchar PWM2 = 19;
uchar PWM3 = 21;
uchar PWM4 = 23;
uchar PWM5 = 25;
uchar PWM6 = 27;
uchar PWM7 = 29;
uchar count=0;
uchar num=0;
float ftemp;
void Timer0(void) interrupt 1 //ET0
{
TH0=(65536-500)/256;
TL0=(65536-500)%256;
count++;
num++;
if(count >= 15) count = 0;
if(count >= PWM7) D0 = 0; else D0 = 1;
if(count >= PWM6) D1 = 0; else D1 = 1;
if(count >= PWM5) D2 = 0; else D2 = 1;
if(count >= PWM4) D3 = 0; else D3 = 1;
if(count >= PWM3) D4 = 0; else D4 = 1;
if(count >= PWM2) D5 = 0; else D5 = 1;
if(count >= PWM1) D6 = 0; else D6 = 1;
if(count >= PWM0) D7 = 0; else D7 = 1;
}
void UartInit(void) //9600bps@11.0592MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器1时钟为Fosc,即1T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设定定时器1为16位自动重装方式
TL1 = 0xE0; //设定定时初值
TH1 = 0xFE; //设定定时初值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
}
void Delay100ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 6;
j = 157;
k = 59;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
unsigned int dat;
// char str[10];
// char i;
UartInit();
P1M0 = 0x00; //设置P1.0为ADC口
P1M1 = 0x01;
ADCCFG = 0x2f; //设置ADC时钟为系统时钟/2/16/16
ADC_CONTR = 0x80; //使能ADC模块
TMOD=0x01; //定时器
TH0=(65536-500)/256; //256个机器周期
TL0=(65536-500)%256;
EA=1;
ET0=1;
TR0=1;
while (1)
{
ADC_CONTR |= 0x40; //启动AD转换 P1.0
_nop_();
_nop_();
while (!(ADC_CONTR & 0x20)); //查询ADC完成标志
ADC_CONTR &= ~0x20; //清完成标志
//ADC_RES(高4位) ADC_RESL(低8位); //读取ADC结果
dat=ADC_RES*256+ADC_RESL;
ftemp=dat;
ftemp=ftemp*3.3/4096;
// sprintf(str,"%.2f\r\n\0",ftemp);
// i=0;
// while(str[i]!=0)
// {
// SBUF=str[i];
// while(!TI);
// TI=0;
// i++;
// }
if(num==50)
{
num=0;
if(ftemp <= 1) {//光照越大,ftemp越小
PWM7++;PWM6++;PWM5++;PWM4++;PWM3++;PWM2++;PWM1++;PWM0++;
if(PWM7==15) PWM7=0;
if(PWM6==15) PWM6=0;
if(PWM5==15) PWM5=0;
if(PWM4==15) PWM4=0;
if(PWM3==15) PWM3=0;
if(PWM2==15) PWM2=0;
if(PWM1==15) PWM1=0;
if(PWM0==15) PWM0=0;
}
else if(ftemp > 1 && ftemp <= 2)
{
PWM7++;PWM6++;PWM5++;PWM4++;PWM3++;PWM2++;PWM1++;PWM0++;
if(PWM7==30) PWM7=0;
if(PWM6==30) PWM6=0;
if(PWM5==30) PWM5=0;
if(PWM4==30) PWM4=0;
if(PWM3==30) PWM3=0;
if(PWM2==30) PWM2=0;
if(PWM1==30) PWM1=0;
if(PWM0==30) PWM0=0;
}
else if(ftemp > 2) //光照越小 ftemp越大
{
PWM7++;PWM6++;PWM5++;PWM4++;PWM3++;PWM2++;PWM1++;PWM0++;
if(PWM7==50) PWM7=0;
if(PWM6==50) PWM6=0;
if(PWM5==50) PWM5=0;
if(PWM4==50) PWM4=0;
if(PWM3==50) PWM3=0;
if(PWM2==50) PWM2=0;
if(PWM1==50) PWM1=0;
if(PWM0==50) PWM0=0;
}
}
// Delay100ms();
}
}
复制代码
流水灯老是断断续续
作者:
liurongbao
时间:
2021-12-30 17:39
试试 在进入定时器中断后 关闭总中断, 结束后再开启总中断
作者:
lkc8210
时间:
2021-12-30 17:53
ADC改用中断查询法
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1