这个PWM可以显示出来,但是目前确实没有想到怎样控制灯光的明暗,如果有哪位大佬直接,欢迎直接留言。下面是源代码
main.c
#include"reg52.h"
#include"intrins.h"
#include"iic.h"
int led_work_mode=1;
int time_blank=4;
int K7=0;
int K6=0;
unsigned char add=0x00;
sbit S7=P3^0;
sbit S6=P3^1;
sbit S5=P3^2;
sbit S4=P3^3;
sfr AUXR=0x8e;
char code xianshi[13]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf,0x7F};
void choose_573(int n)
{
switch(n)
{
case(0):P2=(P2&0x1f)|0x00;break;
case(4):P2=(P2&0x1f)|0x80;break;
case(5):P2=(P2&0x1f)|0xa0;break;
case(6):P2=(P2&0x1f)|0xc0;break;
case(7):P2=(P2&0x1f)|0xe0;break;
}
}
void init_system()
{
choose_573(4);
P0=0xff;
choose_573(5);
P0=0x00;
choose_573(0);
P0=0xff;
}
//==========================IIC
int guangxian;
void get_guangxian()
{
guangxian=read_v();
}
//==========================
//==========================工作模式
void Delay100ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 5;
j = 52;
k = 195;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void work()
{
if(K7==1&&led_work_mode==1)
{
int i;
int t;
for(i=0;i<9;i++)
{
choose_573(4);
P0=(0xff>>i);
choose_573(0);
for(t=0;t<time_blank;t++)
{
Delay100ms();
}
if(K7!=1)
{
break;
break;
}
}
}
if(led_work_mode==2&&K7==1)
{
int i;
int t;
for(i=0;i<9;i++)
{
choose_573(4);
P0=(0xff<<i);
choose_573(0);
for(t=0;t<time_blank;t++)
{
Delay100ms();
}
}
}
if(led_work_mode==3&&K7==1)
{
int i;int t;
for(i=0;i<5;i++)
{
choose_573(4);
P0=(0xff<<i)&(0xff>>i);
choose_573(0);
for(t=0;t<time_blank;t++)
{
Delay100ms();
}
}
}
if(led_work_mode==4&&K7==1)
{
int i;int t;
for(i=5;i>=0;i--)
{
choose_573(4);
P0=~((0xff<<i)&(0xff>>i));
choose_573(0);
for(t=0;t<time_blank;t++)
{
Delay100ms();
}
}
}
}
//==========================
//==========================按键
void Delay5ms() //@11.0592MHz
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}
void key_board()
{
if(S7==0)
{
Delay5ms();
if(S7==0)
{
if(K7==0)
{
K7=1;
}
else
{
K7=0;
}
}
while(!S7);
}
if(S6==0)
{
Delay5ms();
if(S6==0)
{
K6++;
if(K6==3)
{
K6=0;
}
}
while(!S6);
}
if(S5==0&&K6==1)
{
Delay5ms();
if(S5==0&&K6==1)
{
led_work_mode++;
if(led_work_mode>=5)
{
led_work_mode=4;
}
}
while(!S5);
}
if(S4==0&&K6==1)
{
Delay5ms();
if(S4==0&&K6==1)
{
led_work_mode--;
if(led_work_mode<=0)
{
led_work_mode=1;
}
}
while(!S4);
}
if(S5==0&&K6==2)
{
Delay5ms();
if(S5==0&&K6==2)
{
time_blank++;
if(time_blank>=13)
{
time_blank=12;
}
write_EEPROM(add,time_blank);
}
while(!S5);
}
if(S4==0&&K6==2)
{
Delay5ms();
if(S4==0&&K6==2)
{
time_blank--;
if(time_blank<4)
{
time_blank=4;
}
write_EEPROM(add,time_blank);
}
while(!S4);
}
}
//==========================
//==========================数码管
void Delay300us() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 4;
j = 54;
do
{
while (--j);
} while (--i);
}
int shanshuo_mode=0;
void SMG(int wei,int number)
{
choose_573(6);
P0=0x80>>(wei-1);
choose_573(7);
P0=xianshi[number];
choose_573(0);
P0=0xff;
}
void smg_display()
{
if(K6==1)
{
SMG(1,0);
Delay300us();
SMG(2,0);
Delay300us();
SMG(3,time_blank%10);
Delay300us();
if(time_blank<10)
{
SMG(4,10);
Delay300us();
}
else
{
SMG(4,time_blank/10);
Delay300us();
}
SMG(5,10);
Delay300us();
if(shanshuo_mode==0)
{
SMG(6,11);
Delay300us();
SMG(7,led_work_mode);
Delay300us();
SMG(8,11);
Delay300us();
}
else
{
SMG(6,10);
Delay300us();
SMG(7,10);
Delay300us();
SMG(8,10);
Delay300us();
}
}
if(K6==2)
{
if(shanshuo_mode==0)
{
SMG(1,0);
Delay300us();
SMG(2,0);
Delay300us();
SMG(3,time_blank%10);
Delay300us();
if(time_blank<10)
{
SMG(4,10);
Delay300us();
}
else
{
SMG(4,time_blank/10);
Delay300us();
}
SMG(5,10);
Delay300us();
}
else
{
SMG(1,10);
Delay300us();
SMG(2,10);
Delay300us();
SMG(3,10);
Delay300us();
SMG(4,10);
Delay300us();
SMG(5,10);
Delay300us();
}
SMG(6,11);
Delay300us();
SMG(7,led_work_mode);
Delay300us();
SMG(8,11);
Delay300us();
}
if(K6==0&&S4==0)
{
SMG(1,guangxian);
Delay300us();
SMG(2,11);
Delay300us();
SMG(3,10);
Delay300us();
SMG(4,10);
Delay300us();
SMG(5,10);
Delay300us();
SMG(6,10);
Delay300us();
SMG(7,10);
Delay300us();
SMG(8,10);
Delay300us();
}
}
//==========================
//========================== 中断
void Timer0Init(void) //5毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x00; //设置定时初始值
TH0 = 0x28; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0=1;
EA=1;
}
int n=0;
void Timer0Init_server() interrupt 1
{
key_board();
get_guangxian();
if(K6==1||K6==2)
{
n++;
if(n>=160)
{
if(shanshuo_mode==1)
{
shanshuo_mode=0;
}
else
{
shanshuo_mode=1;
}
n=0;
}
}
}
//==========================
void main()
{
init_system();
Timer0Init();
time_blank=read_EEPROM(add);
while(1)
{
work();
smg_display();
}
}
iic.c
/*
程序说明: IIC总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include "reg52.h"
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
//总线启动条件
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//发送应答
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit; // 0:应答,1:非应答
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//等待应答
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
return da;
}
void write_EEPROM(unsigned char add,unsigned char dat)
{
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 temp;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return temp;
}
unsigned char read_v()
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return temp*0.019;
}
iic.h
#ifndef _IIC_H
#define _IIC_H
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
unsigned char read_v();
unsigned char read_EEPROM(unsigned char add);
void write_EEPROM(unsigned char add,unsigned char dat);
#endif