maichanduy
Newbie level 4
- Joined
- Aug 25, 2012
- Messages
- 6
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,326
help me!
output of 74hc595 is low lever but led don't off
please check my code and help me find erros
Code:
/************************************************** ***
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
************************************************** ***/
#include <mega16.h>
#include <delay.h>
#define DATA PORTA.2
#define SCK PORTA.3
#define SCL PORTA.4
#define DATA60 PORTA.5
#define SCK60 PORTA.6
#define SCL60 PORTA.7
unsigned char h,m,s;
unsigned char h1,h2,m1,m2,s1,s2;
unsigned char ma1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x9 0,0xFF};
unsigned char ma2[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6 F,0x00};
unsigned char MALED1[]={0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00};
unsigned char MALED2[]={0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};
unsigned char i,j,k;
// I2C Bus functions
#asm
.equ __i2c_port=0x15 ;PORTC
.equ __sda_bit=1
.equ __scl_bit=0
#endasm
#include <i2c.h>
// DS1307 Real Time Clock functions
#include <ds1307.h>
// Timer 0 overflow interrupt service routine
static unsigned int counter;
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reinitialize Timer 0 value
TCNT0=0x06;
++counter;
}
#define FIRST_ADC_INPUT 0
#define LAST_ADC_INPUT 1
unsigned char adc_data[LAST_ADC_INPUT-FIRST_ADC_INPUT+1];
#define ADC_VREF_TYPE 0x60
// ADC interrupt service routine
// with auto input scanning
interrupt [ADC_INT] void adc_isr(void)
{
static unsigned char input_index=0;
// Read the 8 most significant bits
// of the AD conversion result
adc_data[input_index]=ADCH;
// Select next ADC input
if (++input_index > (LAST_ADC_INPUT-FIRST_ADC_INPUT))
input_index=0;
ADMUX=(FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff))+input_index;
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
}
void Show_TIME(unsigned char x)
{
int i,temp;
for(i=0;i<8;i++)
{
temp=x;//gan bien
temp=temp&0x80;//lay ra bit dau tien (bit 7)
if(temp==0x80)//so sanh bit
DATA=1; //bang 1 thi xuat vao chip =1
else
DATA=0; //nguoc lai bang 0
x*=2; //dich bit lay bit trong so thap
SCK=0; //tao xung tren chan 11
SCK=1; //1 xung dua vào 1 bít
}
//SCL=0;
// SCL=1;
}
void Ghidich(unsigned char x)
{
int i,temp;
for(i=0;i<8;i++)
{
temp=x;//gan bien
temp=temp&0x80;//lay ra bit dau tien (bit 7)
if(temp==0x80)//so sanh bit
DATA60=1; //bang 1 thi xuat vao chip =1
else
DATA60=0; //nguoc lai bang 0
x*=2; //dich bit lay bit trong so thap
SCK60=0; //tao xung tren chan 11
SCK60=1; //1 xung dua vào 1 bít
}
}
void ghi_dich(unsigned char t1,unsigned char t2,unsigned char t3,unsigned char t4,unsigned char t5,unsigned char t6,unsigned char t7,unsigned char t8)
{unsigned char i,Q;
Q=t8; for(i=0;i<8;i++){DATA60=(Q&0x01)?1:0;SCK60=1;SCK60=0;Q>>=1; }
Q=t7; for(i=0;i<8;i++){DATA60=(Q&0x01)?1:0;SCK60=1;SCK60=0;Q>>=1; }
Q=t6; for(i=0;i<8;i++){DATA60=(Q&0x01)?1:0;SCK60=1;SCK60=0;Q>>=1; }
Q=t5; for(i=0;i<8;i++){DATA60=(Q&0x01)?1:0;SCK60=1;SCK60=0;Q>>=1; }
Q=t4; for(i=0;i<8;i++){DATA60=(Q&0x01)?1:0;SCK60=1;SCK60=0;Q>>=1; }
Q=t3; for(i=0;i<8;i++){DATA60=(Q&0x01)?1:0;SCK60=1;SCK60=0;Q>>=1; }
Q=t2; for(i=0;i<8;i++){DATA60=(Q&0x01)?1:0;SCK60=1;SCK60=0;Q>>=1; }
Q=t1; for(i=0;i<8;i++){DATA60=(Q&0x01)?1:0;SCK60=1;SCK60=0;Q>>=1; }
SCL60=0; SCL60=1;
}
void Clear_60LED(void)
{
ghi_dich(0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
}
void Clear_TIME(void)
{
Show_TIME(0x00);
Show_TIME(0x00);
Show_TIME(0x00);
Show_TIME(0x00);
SCL=0;
SCL=1;
}
void Show_LED1(unsigned char gia_tri)
{unsigned char k1,k2,k3;
k1=0;
k2=MALED1[gia_tri%8];
k3=0xFF;
if((gia_tri>=0)&&(gia_tri<8)) ghi_dich(k2,k3,k3,k3,k3,k3,k3,k3);
if((gia_tri>=8)&&(gia_tri<16)) ghi_dich(k1,k2,k3,k3,k3,k3,k3,k3);
if((gia_tri>=16)&&(gia_tri<24))ghi_dich(k1,k1,k2,k3,k3,k3,k3,k3);
if((gia_tri>=24)&&(gia_tri<32))ghi_dich(k1,k1,k1,k2,k3,k3,k3,k3);
if((gia_tri>=32)&&(gia_tri<40))ghi_dich(k1,k1,k1,k1,k2,k3,k3,k3);
if((gia_tri>=40)&&(gia_tri<48))ghi_dich(k1,k1,k1,k1,k1,k2,k3,k3);
if((gia_tri>=48)&&(gia_tri<56))ghi_dich(k1,k1,k1,k1,k1,k1,k2,k3);
if((gia_tri>=56)&&(gia_tri<60))ghi_dich(k1,k1,k1,k1,k1,k1,k1,k2);
}
void Show_LED2(unsigned char gia_tri)
{unsigned char k1,k2,k3;
k1=0;
k2=MALED2[gia_tri%8];
k3=0xFF;
if((gia_tri>=0)&&(gia_tri<8)) ghi_dich(k2,k1,k1,k1,k1,k1,k1,k1);
if((gia_tri>=8)&&(gia_tri<16)) ghi_dich(k3,k2,k1,k1,k1,k1,k1,k1);
if((gia_tri>=16)&&(gia_tri<24))ghi_dich(k3,k3,k2,k1,k1,k1,k1,k1);
if((gia_tri>=24)&&(gia_tri<32))ghi_dich(k3,k3,k3,k2,k1,k1,k1,k1);
if((gia_tri>=32)&&(gia_tri<40))ghi_dich(k3,k3,k3,k3,k2,k1,k1,k1);
if((gia_tri>=40)&&(gia_tri<48))ghi_dich(k3,k3,k3,k3,k3,k2,k1,k1);
if((gia_tri>=48)&&(gia_tri<56))ghi_dich(k3,k3,k3,k3,k3,k3,k2,k1);
if((gia_tri>=56)&&(gia_tri<60))ghi_dich(k3,k3,k3,k3,k3,k3,k3,k2);
delay_ms(5);Clear_60LED();delay_us(200);
}
void TIME(void)
{
m1=m%10;
m2=m/10;
h1=h%10;
h2=h/10;
Show_TIME(ma2[m1]);
Show_TIME(ma2[m2]);
Show_TIME(ma2[h1]);
Show_TIME(ma2[h2]);
SCL=0;
SCL=1;
}
void main(void)
{
// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=In Func0=In
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=T State0=T
PORTA=0xFC;
DDRA=0xFC;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125.000 kHz
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x03;
TCNT0=0x06;
OCR0=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AVCC pin
// ADC Auto Trigger Source: Free Running
// Only the 8 most significant bits of
// the AD conversion result are used
ADMUX=FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff);
ADCSRA=0xEB;
SFIOR&=0x1F;
// I2C Bus initialization
i2c_init();
// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: On
// Square wave frequency: 1Hz
rtc_init(0,1,0);
rtc_set_time(01,59,50);
// Global enable interrupts
#asm("sei")
while (1)
{
rtc_get_time(&h,&m,&s);
Show_LED2(s);
TIME();
}
}