2014年2月3日 星期一

MCU Atmega128 LED 閃爍

//***********************************************************************
//FUNCTION : LED FLASH
/* Test configuration:
MCU: ATmega128

Oscillator: External Clock 07.37280 MHz
Ext. Modules: -
SW: mikroC PRO for AVR
*/

//***********************************************************************
// Include Files
//***********************************************************************
#define uchar unsigned char
#define uint unsigned int

#define Data_IO PORTA //8-LED DATA
#define Data_DDR DDRA //8-LED DATA DIRECTION

//----------------------------------------------------------------------------------------
// Macros
//----------------------------------------------------------------------------------------
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define CLRBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
#define CHKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))

sbit D_LE at PORTD4_bit; // Output pin, PINx register is used
sbit W_LE at PORTD5_bit; // Output pin, PINx register is used
sbit D_LE_Pin_Direction at DDD4_bit;
sbit W_LE_Pin_Direction at DDD5_bit;

//***********************************************************************
// Common cathode DISPLAY CODE 0--F
// DP G F E D C B A
// 0 0 0 1 1 1 1 1 1 ==> 3FH
// 1 0 0 0 0 0 1 1 0 ==> 06H
// 2 0 1 0 1 1 0 1 1 ==> 5BH
//***********************************************************************

uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

//***********************************************************************
// IO PORT Initialization
//***********************************************************************

void System_Init()
{
Data_IO=0xFF; //7-SEGMENT DATA PORT IS OUT <1=OUT , 0=IN>
Data_DDR=0xFF;

D_LE_Pin_Direction = 1; // Set pin as output ;
W_LE_Pin_Direction = 1; // Set pin as output ;
//IC74HC573 CONTROL PORT IS OUT.
//IC1 74LS573 LE=PD4 ,IC2 74LS573 LE=PD5
}

//*************************************************************************
// 74HC573 CONTROL 7-SEGMENT SCAN FUNCTION
// 74LS573 LE=1 ACTIVE , LE=0 DISABLE
//*************************************************************************

void Display_char(uchar num)
{
D_LE=1;
W_LE=1; //控制數碼管位的74HC573的LE管腳置高
Data_IO=0x7f; //設置要顯示的位,也就是哪一個數碼管亮,這裡是八個一起顯示
// Display @ digit 0 , all segment 8 digits , 7-6-5-4-3-2-1-0
Delay_us(5);
W_LE=0; //鎖存位數據,下面送上段數據以後,就顯示出來了
Data_IO=table[num]; //SEND DATA TO 7-SEG, EXAMPLE 0 IS SEND 0x3f
D_LE=0; //鎖存段數據,數碼管亮一個時間片刻
Delay_us(10); //DELAY, affect the brightness and flashing of 7-SEG
}

//*************************************************************************
// MAIN PROCEDURE
//*************************************************************************

void main(){
char num;
System_Init();
while(1) {
for (num=0;num<=15;num++)
{
Display_char(num);
Delay_ms(250);
}
} ;
} // END MAIN

沒有留言:

張貼留言