Markdem
Joined: 24 Jun 2005 Posts: 206
|
7 segment driver chip |
Posted: Mon Aug 24, 2009 10:26 pm |
|
|
Hi All, Doing a project with a stack of 7 segments, I have decided to make my own driver chip, cheaper the buying the maxim parts.
Here is my code;
Code: |
#include <18F4680.h>
#fuses EC_IO,NOWDT,NOPROTECT,NOLVP,NODEBUG,NOMCLR
#use delay(clock=4000000)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0xa0)
const char digit[10]={
//0 1 2 3 4 5 6 7 8 9
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90
};
int display_string[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; //this is the last 4 digets of both displays, leading zero is not used
int index = 1;
int out_port = 0b11111110;
#INT_SSP
void i2c_isr()
{
int state, incoming, address;
state = i2c_isr_state();
if(state < 0x80) //Master is sending data
{
incoming = i2c_read();
if(state == 0) //First received byte is address
address = incoming;
if(state == 1) //Second received byte is data
display_string[address] = incoming;
}
}
#int_rtcc
void clock_isr()
{
// delay_ms(500); //test delay only
output_b(0xFF);
output_d(digit[display_string[index]]);
output_b(out_port);
delay_ms(1);
if(index >= 8)
{
index = 1;
out_port = 0b11111110;
}
else
{
index++;
rotate_left(&out_port,1);
}
}
void main(void)
{
output_b(0xFF);
output_d(0xFF);
setup_counters(RTCC_INTERNAL, RTCC_DIV_16 | RTCC_8_BIT);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(1)
{
delay_us(1);
}
}
|
It works, at least in simulation, but I was just wondering if there is anything i could\should improve or change.
Thanks
mark |
|