彩灯控制器
2019-04-14 20:15发布
生成海报
1.题目:
## 注意:时序要求严格,读取eeprom和AD值要关中断!
2.代码:
main.c
******************************************************************************
* 文件名:彩灯控制器
* 描 述:
* 作 者:思索与猫
* 日 期: 19/3/18
* 备 注:
*
******************************************************************************
#include
#include
uchar pwm;
uchar ad_value;
void main()
{
CloseFucker();
Timer0Init();
Timer1Init();
init_pcf859();
time = read_eeprom(0x01); //读取eeprom
while(1)
{
if(flag_ad == 1) //读AD值
{
flag_ad = 0;
ad_value = read_pcf859();
ad_value = ad_value*99/255.0f;
if(ad_value <= 24)
{
pwm = 1;
}
else if(ad_value>24&&ad_value <= 49)
{
pwm = 2;
}
else if(ad_value>49&&ad_value <= 74)
{
pwm = 3;
}
else if(ad_value>74&&ad_value <= 99)
{
pwm = 4;
}
}
KeyDriver();
ShowTable();
}
}
sys.h
#ifndef __SYS_H_
#define __SYS_H_
#include
#include
typedef unsigned char uchar;
typedef unsigned int uint;
//初始
void Timer0Init();
void Timer1Init();
void CloseFucker();
uchar LedMode();
//显示
void Display();
void ShowTable();
void ShowLed(uchar temp);
//按键
void KeyDriver();
void KeyAction(uchar key_value);
void KeyScan();
extern uchar code duan[];
extern uchar Table[];
extern uchar mode,time,choose;
extern uchar led_dat;
extern uchar index;
extern bit flag_set,flag_smg,flag_start,flag_ad,flag_level;
extern uchar ad_value;
extern uchar pwm;
#endif
sys.c
#include
uchar led_dat = 0x00;
uchar index = 0;
bit flag_smg,flag_ad;
void Timer0Init() //2ms@12.000MHz
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x40;
TH0 = 0xA2;
TF0 = 0;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void Timer1Init() //100us@12.000MHz
{
AUXR |= 0x40;
TMOD &= 0x0F;
TL1 = 0x50;
TH1 = 0xFB;
TF1 = 0;
TR1 = 1;
ET1 = 1;
}
void T0_time() interrupt 1
{
static uint count = 0,smg_count = 0,ad_count = 0;
TL0 = 0x40;
TH0 = 0xA2;
Display();
KeyScan();
if(++ad_count == 30) //60ms读取AD值
{
ad_count = 0;
flag_ad = 1;
}
if(flag_set == 1&&++smg_count>=400) //数码管0.8S闪烁
{
smg_count = 0;
flag_smg = ~flag_smg;
}
if(flag_start == 1&&++count >= time*50) //间隔时间
{
count = 0;
led_dat = LedMode();
}
}
void T1_time() interrupt 3
{
static uint time = 0;
uchar hightime;
TL1 = 0x50;
TH1 = 0xFB;
time++;
time &= 0x0f; //最高1500us
hightime = pwm*pwm; //高电平时间
if(time < hightime)
{
ShowLed(led_dat);
}
else
{
ShowLed(0x00);
}
}
uchar LedMode()
{
uchar led;
uchar left,right;
if(mode == 1)
{
led = 0x01;
led = led<= 8) index = 0;
}
else if(mode == 2)
{
led = 0x80;
led = led>>index;
if(++index >= 8) index = 0;
}
else if(mode == 3)
{
left = 0x01;right = 0x80;
left = left <>index;
led = left|right;
if(++index >= 4) index = 0;
}
else if(mode == 4)
{
left = 0x10;right = 0x08;
left = left <>index;
led = left|right;
if(++index >= 4) index = 0;
}
return led;
}
void CloseFucker()
{
P2 = P2&0x1f|0xa0;
P0 = 0xaf;
P2 = 0x1f;
}
display.c
#include
uchar code duan[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x40}; //10black,11-
uchar Table[8];
uchar mode = 1,time = 4;
bit flag_level;
void Display()
{
static uchar index = 0;
P2 = P2&0x1f|0xe0;
P0 = 0xff;
P2 = 0x1f;
P2 = P2&0x1f|0xc0;
P0 = 1<
key.c
#include
sbit Key_In_1 = P3^0;
sbit Key_In_2 = P3^1;
sbit Key_In_3 = P3^2;
sbit Key_In_4 = P3^3;
uchar KeySta[4] = {1, 1, 1, 1};
uchar KeyCodeMap[4] = {7, 6, 5, 4};
uchar choose = 0;
bit flag_set,flag_start;
void KeyDriver()
{
uchar i = 0;
static uchar KeyBack[4] = {1, 1, 1, 1};
for(i=0; i<4; i++)
{
if(KeySta[i] != KeyBack[i])
{
if(flag_set == 0&&KeySta[3] == 0) //按下S4不松手,亮度界面
{
flag_level = 1;
}
if(KeySta[3] == 1) //松开S4
{
flag_level = 0;
}
if(KeySta[i] != 0)
{
KeyAction(KeyCodeMap[i]);
}
}
KeyBack[i] = KeySta[i];
}
}
void KeyAction(uchar key_value)
{
if(key_value == 7) //led开关
{
flag_start = ~flag_start;
index = 0;
led_dat = 0x00;
}
else if(key_value == 6)
{
flag_set = 1;
if(++choose == 3)
{
led_dat = 0x00;
choose = 0;
flag_set = 0;
flag_start = 0;
index = 0;
switch(mode) //存储各模式间隔时间
{
case 1:write_eeprom(time,0x01);break;
case 2:write_eeprom(time,0x02);break;
case 3:write_eeprom(time,0x03);break;
case 4:write_eeprom(time,0x04);break;
}
}
}
else if(flag_set == 1&&key_value == 5)
{
if(choose == 1)
{
mode++;
switch(mode) //读取各模式间隔时间
{
case 1:time = read_eeprom(0x01);break;
case 2:time = read_eeprom(0x02);break;
case 3:time = read_eeprom(0x03);break;
case 4:time = read_eeprom(0x04);break;
}
if(mode >= 4) mode =4;
}
else if(choose == 2)
{
time++;
if(time >= 12) time = 12;
}
}
else if(flag_set == 1&&key_value == 4)
{
if(choose == 1)
{
mode--;
switch(mode) //读取各模式间隔时间
{
case 1:time = read_eeprom(0x01);break;
case 2:time = read_eeprom(0x02);break;
case 3:time = read_eeprom(0x03);break;
case 4:time = read_eeprom(0x04);break;
}
if(mode <= 1) mode = 1;
}
else if(choose == 2)
{
time--;
if(time <= 4) time = 4;
}
}
}
void KeyScan()
{
uchar i = 0;
static uchar KeyBuf[4] = {0xff, 0xff, 0xff, 0xff};
KeyBuf[0] = KeyBuf[0]<<1|Key_In_1 ;
KeyBuf[1] = KeyBuf[1]<<1|Key_In_2 ;
KeyBuf[2] = KeyBuf[2]<<1|Key_In_3 ;
KeyBuf[3] = KeyBuf[3]<<1|Key_In_4 ;
for(i=0; i<4; i++)
{
if(KeyBuf[i] == 0xff)
{
KeySta[i] = 1;
}
else if(KeyBuf[i] == 0x00)
{
KeySta[i] = 0;
}
else
{
}
}
}
iic.h
#ifndef _IIC_H
#define _IIC_H
#include "stc15f2k60s2.h"
#include "intrins.h"
#define somenop Delay5us()
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
sbit SDA = P2^1; /* Êý¾ÝÏß */
sbit SCL = P2^0; /* ʱÖÓÏß */
void IIC_Start(void);
void IIC_Stop(void);
void IIC_Ack(unsigned char ackbit);
void IIC_SendByte(unsigned char byt);
bit IIC_WaitAck(void);
unsigned char IIC_RecByte(void);
void Delay5us();
void write_eeprom(unsigned char dat,unsigned char add);
unsigned char read_eeprom(unsigned char add);
void init_pcf859();
unsigned char read_pcf859();
#endif
iic.c
#include "iic.h"
void Delay5us() //@12.000MHz
{
unsigned char i;
_nop_();
_nop_();
i = 12;
while (--i);
}
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
somenop;
SDA = 0;
somenop;
SCL = 0;
}
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
somenop;
SDA = 1;
}
void IIC_Ack(unsigned char ackbit)
{
if(ackbit)
{
SDA = 0;
}
else
{
SDA = 1;
}
somenop;
SCL = 1;
somenop;
SCL = 0;
SDA = 1;
somenop;
}
bit IIC_WaitAck(void)
{
SDA = 1;
somenop;
SCL = 1;
somenop;
if(SDA)
{
SCL = 0;
IIC_Stop();
return 0;
}
else
{
SCL = 0;
return 1;
}
}
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(byt&0x80)
{
SDA = 1;
}
else
{
SDA = 0;
}
somenop;
SCL = 1;
byt <<= 1;
somenop;
SCL = 0;
}
}
unsigned char IIC_RecByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i<8;i++)
{
SCL = 1;
somenop;
da <<= 1;
if(SDA)
da |= 0x01;
SCL = 0;
somenop;
}
return da;
}
void write_eeprom(unsigned char dat,unsigned char add)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
unsigned char read_eeprom(unsigned char add)
{
unsigned char dat;
EA = 0; //注意,这里对时序要求更加严格,关中断读eeprom
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
dat = IIC_RecByte();
IIC_Ack(0);
IIC_Stop();
EA = 1;
return dat;
}
void init_pcf859()
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
}
unsigned char read_pcf859()
{
unsigned char dat;
EA = 0; //与上同理
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
dat = IIC_RecByte();
IIC_Ack(0);
IIC_Stop();
EA = 1;
return dat;
}
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮