| |||||||
Опубликовано 2010-02-27 09:55:03 автором MRS Connection to a microcontroller seven-segment displayIn the previous lesson we have learned flashing led. This lesson slightly harder here we'll learn how to flash 7 LEDs. But not just seven LEDs, and segment indicator ![]() seven-segment indicators - LEDs placed in a certain order, so that the lighting multiple LEDs, you can build on the indicator symbols, and numbers. The indicators are common cathode (less) and common anode (plus). This means that all seven LEDs are connected by a plus or a minus. For example, consider the indicator with common cathode and try it something to, for example the number 1. To do this, we need to enable the LEDs B and C. Take our atmega8 and connect to семисегментнику scheme
#include <mega8.h> void main(void) { PORTB=0x00; DDRB=0b00000001; //do the leg PB0 output PORTD is set=0b00000110; // print on the legs of PB1 and PB2 log unit to ignite the number 1 DDRD=0b01111111; //Make legs PB0-PB6 outputs while (1) {} } Look at the result and rejoice! And what to bring any digit from 0 to 9? To do this, we will have a little bit complicate our program. Write a procedure out to output numbers from 0 to 9. #include <mega8.h> #include <delay.h> unsigned char numder[]= //define an array, where the index will correspond to the output digit { 0b00111111, //0 0b00000110, //number 1 0b01011011, //number 2 0b01001111, //number 3 0b01100110, //number 4 0b01101101, //number 5 0b01111101, //figure 6 0b00000111, //number 7 0b01111111, //number 8 0b01101111, //number 9 }; void out(unsigned char num) { PORTD is set=numder[num]; } void main(void) { unsigned char i=0; PORTB=0x00; DDRB=0b00000001; //do the leg PB0 output PORTD is set=0b0000000; DDRD=0b01111111; //Make legs PB0-PB6 outputs while (1) { out(i++); delay_ms(1000);//delay for 1 second } } Result #include <mega8.h> #include <delay.h> unsigned char numder[]= //define an array, where the index will correspond biti on port D { 0b00111111, //0 0b00000110, //number 1 0b01011011, //number 2 0b01001111, //number 3 0b01100110, //number 4 0b01101101, //number 5 0b01111101, //figure 6 0b00000111, //figure 7 0b01111111, //number 8 0b01101111, //number 9 }; void out(unsigned char num) { PORTD is set=numder[num]; } void main(void) { unsigned char z=0; PORTB=0x00; DDRB=0b00000011; //do the leg PB0 and PB1 output PORTD is set=0b0000000; DDRD=0b01111111; //Make legs PB0-PB6 outputs while (1) { if (z==0) //if z=0, we deduce the first digit of the { out(1); //print 1 PORTB=0b00000001; // switch семисегментник that hangs cathode on the leg PB0 } if (z==1) //if z=1, we derive the second figure { out(7); // display the digit 7 PORTB=0b00000010; // Switch семисегментник on the leg PB1 } if (++z>1) z=0; // increase the z on the unit and check to see if it 1. If not, the z record 0 delay_ms(40); //Make the delay in 40 milliseconds, this 25 of the conclusions of the second } } Let's all this приаккуратим:
#include <mega8.h> #include <delay.h> unsigned char chislo[2]; //define an array of two elements of type char unsigned(unsigned) unsigned char numder[]= //define an array, where the index will correspond biti on port D { 0b00111111, //0 0b00000110, //number 1 0b01011011, //number 2 0b01001111, //number 3 0b01100110, //number 4 0b01101101, //number 5 0b01111101, //figure 6 0b00000111, //number 7 0b01111111, //number 8 0b01101111, //number 9 }; unsigned char i=0; // variable to determine what rank семисегментника output the number of // interrupts overflow timer0 interrupt [TIM0_OVF] void timer0_ovf_isr(void) { PORTD is set=numder[ chislo[i] ]; PORTB=i+1; //switch a certain category of семисегментника if (++i>1) i=0; } void out(unsigned char num) { /*first we need to determine how many tens and units is displayed number. For example, we passed "45", we need to lay it out on the 4 and 5 to the first decimal семисегментника display figure 4, and the other 5 */ chislo[0]=num%10; //the % operator gives the remainder of integer division, for example, 34%10 will be 4 chislo[1]=num/10; // find out how many tens in the number of } void main(void) { unsigned char z=0; // variable that will be displayed PORTB=0x00; DDRB=0b00000011; //do the leg PB0 and PB1 output PORTD is set=0b0000000; DDRD=0b01111111; //Make legs PB0-PB6 outputs TCCR0=0x03; //set the timer prescaler TCNT0=0x64; // number which the timer begins to count up to 255 TIMSK=0x01; // overflow interrupt timer 0 #asm("sei") //allow interrupts while (1) { out(z++); // print the value of variable z and increase it by 1 delay_ms(1000); //Make the delay in 40 milliseconds, this 25 of the conclusions of the second } } In the next article will discuss how to the microcontroller mount lcd display. Комментарии - (4)
Добавить комментарийДля отправки комментария вы должны авторизоваться. |