1. 程式人生 > 其它 >藍橋杯微控制器 第九屆 彩燈控制器

藍橋杯微控制器 第九屆 彩燈控制器

技術標籤:藍橋杯

藍橋杯微控制器 第九屆 彩燈控制器

main.c

#include<STC15F2K60S2.H>
#include<iic.h>
#define u8 unsigned char
#define u16 unsigned int
#define keypress P3 
sbit pwm = P0^0;
sbit buzzer = P0^6;
sbit relay = P0^4;
bit smg_flag, keyflag, LED_flag = 0 ;
bit s4_smgflag, ms20_flag;
u16 volt,
ms20_count; u8 v; u8 yx_index=1; //執行模式 u16 times[5] = {400,400,400,400,400}, smg_times = 800;//流轉間隔 bit times_flag, smg_s_flag = 1; u8 trg,cont,s6_mode = 0 ; u8 table1[8] , table2[8]; u8 class_light , pwm_count ;//亮度等級 u8 code D_display[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; u8 code W_display[]={0x01,0x02,0x04,0x08,0x10,0x20
,0x40,0x80}; typedef unsigned char BYTE; typedef unsigned int WORD; void lint(); void smg_disp(); void key_scan(); void led_mode1(); //執行模式1的led燈亮的方式 void led_mode2(); void led_mode3(); void led_mode4(); void led_light(); void s5();//s5的按鍵功能 void s4(); void s4_0();//熄滅時,cont情況專用 void record_times();//eeprom void
Timer1Init(void) //5ms { AUXR |= 0x40; //?????1T?? TMOD &= 0x0F; //??????? TL1 = 0x00; //?????? TH1 = 0x28; //?????? TF1 = 0; //??TF1?? TR1 = 1; //???1???? } //void Timer1Init(void) //[email protected] //{ // AUXR |= 0x40; //?????1T?? // TMOD &= 0x0F; //??????? // TL1 = 0xC9; //?????? // TH1 = 0xFF; //?????? // TF1 = 0; //??TF1?? // TR1 = 1; //???1???? //} void Delay5ms() //@11.0592MHz { unsigned char i, j; i = 54; j = 199; do { while (--j); } while (--i); } //----------------------------------------------- /* define constants */ #define FOSC 11059200L #define T1MS (65536-FOSC/12/1000) //1ms timer calculation method in 12T mode /* define SFR */ sbit TEST_LED = P1^0; //work LED, flash once per second /* define variables */ WORD times_count; //1000 times counter //----------------------------------------------- /* Timer0 interrupt routine */ u8 smg_i, smg_count, key_count; void tm0_isr() interrupt 1 using 1 { TL0 = T1MS; //reload timer0 low byte TH0 = T1MS >> 8; //reload timer0 high byte if (times_count-- == 0) { times_count = times[yx_index]; times_flag = 1;//達到時間間隔 } if(smg_times-- == 0) { smg_times = 800; smg_s_flag = ~smg_s_flag ; } if (key_count-- == 0) { key_count = 10; keyflag =1; } if (smg_count-- == 0) { smg_count = 1; smg_flag = 1; } if(pwm_count-- == 0) { pwm_count = 15; } if (ms20_count-- == 0) { ms20_count = 50; ms20_flag =1; } } //----------------------------------------------- /* main program */ void main() { TMOD = 0x01; //set timer0 as mode1 (16-bit) TL0 = T1MS; //initial timer0 low byte TH0 = T1MS >> 8; //initial timer0 high byte TR0 = 1; //timer0 start running ET0 = 1; //enable timer0 interrupt EA = 1; //open global interrupt switch // count = 0; //initial counter lint(); EA=0; pcfwrite(); AT24C02write(0x01,4); Delay5ms(); AT24C02write(0x02,4); Delay5ms(); AT24C02write(0x03,4); Delay5ms(); AT24C02write(0x04,4); Delay5ms(); times[1] = AT24C02read(0x01)*100; times[2] = AT24C02read(0x02)*100; times[3] = AT24C02read(0x03)*100; times[4] = AT24C02read(0x04)*100; EA=1; while (1) { smg_disp(); if(ms20_flag) { ms20_flag = 0; EA=0; v= pcfread(); EA=1; } record_times(); volt = v; if(keyflag) { if(volt<64) class_light = 1; if(volt<64*2 && volt>64) class_light = 2; if(volt<64*3 && volt>64*2) class_light = 3; if(volt<255 && volt>64*3) class_light = 4; keyflag = 0; key_scan(); //s4_0(); switch(trg) { case 0x01: LED_flag = ~LED_flag; break;//s7 case 0x02: s6_mode++; if(s6_mode == 3){ s6_mode = 0; }break;//s6 case 0x04: s5(); break;//s5 case 0x08: s4();break;//s4 } led_light(); //(S7和)總的控制LED燈的程式 } } } void lint()//close buzz,relay { P2=0xa0; buzzer =0 ; relay = 0; P2=0x00; } void key_scan() { u8 readdat; readdat = keypress^0xff; trg = readdat&(readdat^cont); cont = readdat; } void led_light() //(S7和)總的控制LED燈的程式 { if(LED_flag && pwm_count < class_light*class_light) { switch(yx_index) { case 1:led_mode1();break; case 2:led_mode2();break; case 3:led_mode3();break; case 4:led_mode4();break; } } else { P2 = (P2&0x1f)|0x80; P0=0xff; P2 = (P2&0x1f); } } void led_mode1() { u8 i; if(times_flag) { times_flag = 0; P2 = (P2&0x1f)|0x80; P0 = ~(0x01<<i); P2 = (P2&0x1f); i++; if(i == 8) i = 0; } } void led_mode2() { u8 i; //P2=0xa0;relay = 1; buzzer =0; P2=0x00; if(times_flag) { times_flag = 0; P2 = 0x80; P0 = ~(0x80>>i); P2=0x00; i++; if(i == 8) i = 0; } } void led_mode3() { u8 i; //P2=0xa0;relay = 1; buzzer =0; P2=0x00; if(times_flag) { times_flag = 0; P2 = 0x80; P0 = ~((0x80>>i)|(0x01<<i)); P2=0x00; i++; if(i == 4) i = 0; } } void led_mode4() { u8 i; //P2=0xa0;relay = 1; buzzer =0; P2=0x00; if(times_flag) { times_flag = 0; P2 = 0x80; P0 = ~((0x08>>i)|(0x10<<i)); P2=0x00; i++; if(i == 4) i = 0; } } void smg_disp() { if(smg_flag) { smg_flag = 0; if(s6_mode !=0) { if(smg_s_flag == 1 && s6_mode ==1) { //smg_s_flag = 0; table1[0]=0x00; table1[1]=0x00; table1[2]=0x00; } else if( smg_s_flag ==1 && s6_mode == 2) { //smg_s_flag = 0; table1[4]=0x00; table1[5]=0x00; table1[6]=0x00; table1[7]=0x00; } else { table1[0]=0x40; table1[1]=D_display[yx_index%10]; table1[2]=0x40; table1[3]=0x00; table1[4]=D_display[times[yx_index]/1000%10]; table1[5]=D_display[times[yx_index]/100%10]; table1[6]=D_display[times[yx_index]/10%10]; table1[7]=D_display[times[yx_index]%10]; } } s4_0(); if( s6_mode != 0) { P2 =(0xc0); P0=W_display[smg_i]; P2 =0x00; P2 =(0xe0); P0=~table1[smg_i]; P2 =0x00; } else { P2 =(0xc0); P0=W_display[smg_i]; P2 =0x00; P2 =(0xe0); P0=~table2[smg_i]; P2 =0x00; } smg_i++; if(smg_i == 8) smg_i = 0; // } } } void s5() { if(s6_mode == 1) { yx_index++; if(yx_index == 5) yx_index = 1;//執行模式++ } if(s6_mode == 2) { times[yx_index] = times[yx_index] +100; if(times[yx_index] == 1300) times[yx_index] = 400; } } void s4() { if( s6_mode == 0) { s4_smgflag = 1; } else { s4_smgflag = 0; if(s6_mode == 1) { yx_index--; if(yx_index == 0) yx_index = 4;//執行模式++ } if(s6_mode == 2) { times[yx_index] = times[yx_index]-100; if(times[yx_index] == 300) times[yx_index] = 1200; } } } void s4_0() // { if(s6_mode == 0) { if(cont == 0x08) { table2[0]=0x00; table2[1]=0x00; table2[2]=0x00; table2[4]=0x00; table2[5]=0x00; table2[6]=0x40; table2[7]=D_display[class_light%10]; } else { table2[0]=0x00; table2[1]=0x00; table2[2]=0x00; table2[4]=0x00; table2[5]=0x00; table2[6]=0x00; table2[7]=0x00; } } } void record_times() { EA=0; switch(yx_index) { case 1: AT24C02write(0x01,times[1]/100);break; case 2: AT24C02write(0x02,times[2]/100);break; case 3: AT24C02write(0x03,times[3]/100);break; case 4: AT24C02write(0x04,times[4]/100);break; default : break; } EA=1; }

iic.c

/*
  程式說明: IIC匯流排驅動程式
  軟體環境: Keil uVision 4.10 
  硬體環境: CT107微控制器綜合實訓平臺 8051,12MHz
  日    期: 2011-8-9
*/

#include "reg52.h"
#include "intrins.h"
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}    

#define u16 unsigned int
#define u8 unsigned char
#define DELAY_TIME 5

#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1

//匯流排引腳定義
sbit SDA = P2^1;  /* 資料線 */
sbit SCL = P2^0;  /* 時鐘線 */
void IIC_Ack(bit ackbit)
{
	if(ackbit) 
	{	
		SDA = 0;
	}
	else 
	{
		SDA = 1;
	}
	somenop;
	SCL = 1;
	somenop;
	SCL = 0;
	SDA = 1; 
	somenop;
}

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 pcfwrite()
{
	IIC_Start();
//	IIC_WaitAck();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x03);
	IIC_WaitAck();
	IIC_Stop();	
}

u8 pcfread()
{
	u8 temp;
	IIC_Start();
	//IIC_WaitAck();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x03);
	IIC_WaitAck();
	IIC_Stop();	
	
	IIC_Start();
	//IIC_WaitAck();
	IIC_SendByte(0x91);
	IIC_WaitAck();
  temp =	IIC_RecByte();
	IIC_Stop();	
	return temp;
}

void AT24C02write(u8 add,u8 dat)
{
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();	
	IIC_Stop();	
}
u8 AT24C02read(u8 add)
{
	u8 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_WaitAck();	
	IIC_Stop();	
	return temp;
}

//unsigned char AT24C02read(u8 add)
//{
//  unsigned char d;
//	
//	IIC_Start();
//	IIC_SendByte(0xa0); 	//??????
//	IIC_WaitAck();
//	IIC_SendByte(add);		//????????
//	IIC_WaitAck();
//	IIC_Stop();
//	
//	IIC_Start();
//	IIC_SendByte(0xa1);		//?????
//	IIC_WaitAck();
//	d = IIC_RecByte();	//????
//	IIC_Ack(0);
//	IIC_Stop();
//	return d;
//}

//void AT24C02write(u8 add,u8 dat)
//{
//	EA = 0;
//  IIC_Start();
//  IIC_SendByte(0xa0);	//??????
//  IIC_WaitAck();
//  IIC_SendByte(add);	//??????
//  IIC_WaitAck();
//  IIC_SendByte(dat);	//????
//  IIC_WaitAck();
//  IIC_Stop();
//  somenop;
//	EA = 1;
//}

//	

iic.h

#ifndef _IIC_H
#define _IIC_H
#define u16 unsigned int
#define u8 unsigned char
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); 
void pcfwrite();
u16 pcfread();
void AT24C02write(u8 add,u8 dat);
u8 AT24C02read(u8 add);
#endif