Опубликовано 2010-02-27 09:55:03 автором MRS

Connection to a microcontroller seven-segment display


In 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 indicatorsegment 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 wiring семисегментника to the MC Write a simple program that prints the figure 1

#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 conclusion on the segment indicator Seven-segment indicator can be managed statically and dynamically. Static management information on discharges is displayed continuously, until then, we have considered a static control. Dynamic control is a sequential ignition positions in the indicator at a frequency that is not perceived by the human eye. Connection diagram in this case looks like this dynamic output on the segment indicator Now in our code we change just one line of DDRB=0b00000001; DDRB=0b00000011; //do the leg PB0 and PB1 output, and see the result. dynamic output on семисегментник We both categories displays the same numbers, and we need to, for example, on the first display number 1, and the second figure 7. When first we'll use the number "one", the second grade when this is turned off. In the next moment turn off first, and the second displaying the number "7". To disable alternately the first and second categories, we will identify the different log levels on PD0 and PD1.
#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 приаккуратим:
  1. Do, that we might call a procedure out, passing it any number from 0 to 99, and she will decide on which segment which digit display
  2. Remove the logic display of the main loop and put it in the таймер, for example on timer0
#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)

  • Сергей говорит:
    Все примеры замечательно работают как на симуляторе, так и в железе. Вопрос, как дописать программу динамического вывода символов для 4-х разрядного индикатора и с повторяющимся циклом. Очень нужно. Заранее спасибо.
  • AdminRu говорит:
    Все делается по аналогии. Узнать сколько тысяч сотен десятков и единиц, и вывести их.
  • dethdron говорит:
    Спасибо получилось, а как привязать к кнопкам ? делаю программу при нажатии на кнопку-1 светодиод-1 горит при отпускании гаснет,при нажатии на кнопку-2 светодиод-2 горит при отпускании гаснет. Со светодиодами работает а как сделать так же что бы на индикаторе нажал один горит 1 нажал два горит 2? изначально у меня так #include <mega8.h> void main (void) { DDRD = 0x00; //порт D - вход PORTD = 0xFF; //подключаем нагрузочный резистор DDRB = 0xFF; //порт B - выход PORTB = 0x00; //устанавливаем 0 на выходе while(1) { PORTB = ~PIND; //~ знак поразрядного инвертирования } }
    • AdminRu говорит:
      Нужно в цикл While добавить проверку на нажатия кнопки, если нажата то выводить номер нажатой кнопки, например так
      while (1)
      {
      if (PIND.0==0) out(1);
      if (PIND.1==0) out(2);
      delay_ms(1000); //Делаем задержку в 40 милисекунд, это 25 выводов за секунду
      }
      }

Добавить комментарий

Для отправки комментария вы должны авторизоваться.