Hy
I have a HP488 development board with PIC16F88 mcu.
I try to make a program who count from 0 to 9999 with a delay using Timer TMR0.
My problem is I am not able to make this work.
The program must do the next tasks : count from 0 to 9 --> digit 3 and when it reaches 9 must enable digit 2.
If the count it reaches to 99 must enable digit 3 and so on.
I know how to enable the digits, i read it elctrical drawings. I failed when I try to implement it.
Can somebody give me some tips.
Look at my code.
Thank you.
#include <system.h>
#pragma CLOCK_FREQ 8000000
#pragma DATA _CONFIG, _CP_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC & _LVP_OFF
void setup_hardware(void)
{
trisb = 0x00;
trisa = 0xf0;
intcon = 0b10100000;
option_reg = 0b00000111;
}
unsigned char patterns[10] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x83, 0xf8, 0x80, 0x98};
unsigned char led_counter = 0;
unsigned char led_patterns[4] = {0x08, 0x0C, 0x0E, 0x0F};
unsigned char led = 0;
void display(void)
{
porta = 0;
portb = patterns[led_counter++];
porta = led_patterns[led];
if (led_counter > 9)
led_counter = 0;
}
void display_digit(unsigned char digit, unsigned char pos)
{
led_patterns[pos] = patterns[digit] ;
}
unsigned char counter = 0;
void interrupt(void)
{
if(intcon & 0x04)
{
clear_bit( intcon, 2 );
counter++;
if(counter == 15)
{
counter = 0;
display();
}
}
}
void main ( void )
{
setup_hardware() ;
while(1)
{
}
}
7 segment display
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: 7 segment display
Hello,
The 7-seg display needs to be multiplexed constantly to allow you to display more then a single digit.
Therefore you might be better off using 2 timers. 1 to increment the count which is displayed and the other to drive the display. You could get away with one timer and simply doing the multiplexing in a while loop as part of main.
Each segment needs to be enabled in turn so you would do something like this.
output character 0 to portb
enable 7-seg 0
wait a short delay - 5ms works well
disable 7-seg 0
output character 1 to portb
enable 7-seg 1
wait a short delay - 5ms works well
disable 7-seg 1
output character 2 to portb
enable 7-seg 2
wait a short delay - 5ms works well
disable 7-seg 2
output character 3 to portb
enable 7-seg 3
wait a short delay - 5ms works well
disable 7-seg 3
The 7-seg display needs to be multiplexed constantly to allow you to display more then a single digit.
Therefore you might be better off using 2 timers. 1 to increment the count which is displayed and the other to drive the display. You could get away with one timer and simply doing the multiplexing in a while loop as part of main.
Each segment needs to be enabled in turn so you would do something like this.
output character 0 to portb
enable 7-seg 0
wait a short delay - 5ms works well
disable 7-seg 0
output character 1 to portb
enable 7-seg 1
wait a short delay - 5ms works well
disable 7-seg 1
output character 2 to portb
enable 7-seg 2
wait a short delay - 5ms works well
disable 7-seg 2
output character 3 to portb
enable 7-seg 3
wait a short delay - 5ms works well
disable 7-seg 3
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel