欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
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://m.raoushi.com/bbs/)
Powered by Discuz! X3.1