Ok, so after much head scratching i've come to the guru's for help!!!
So heres the picture, i am writing (or trying to write!) a nixie clock. i need to have four data lines and 6 control lines as its a common anode design, 6 digits and the 74141 chip i'm using decodes 4 bits of data. As i turn on one control line it allow current to flow through one of the tubes, through the 74141 abnd turn on the relevant digit.
So, i figure i need a few functions. one to cycle through the control lines, one to work out which number to put on the tube depending on the time, and one to ouput that number on the data lines.
What i would like to know is how i can use a for loop to automatically cycle through the control / data lines. there must be a way!!! heres what i have got so far:
#include#include #include #define bit_get(p,m) ((p) & (m)) #define bit_set(p,m) ((p) |= (m)) #define bit_clear(p,m) ((p) &= ~(m)) #define bit_flip(p,m) ((p) ^= (m)) #define bit_write(c,p,m) (c ? bit_set(p,m) : bit_clear(p,m)) #define BIT(x) (0x01 << (x)) #define LONGBIT(x) ((unsigned long)0x00000001 << (x)) #define CON_PORT PORTC #define DATA_PORT PORTD #define SECS 0 #define SECS_TEN 1 #define MINS 2 #define MINS_TEN 3 #define HRS 4 #define HRS_TEN 5 int i; char req; int sec = 0; int min = 0; int hr = 0; int C[6]; char port; int num_to_send; int num; //prototypes int get_num(char requested); // MAIN PROGRAM int main (void) { for (;;) { for (i = 0; i <=5; i++) { //turn on Control line //bit_set(CON_PORT; C[i]); // Get num to display switch (i) { case 0 : num = get_num(SECS); //Get Sec Unit case 1 : num = get_num(SECS_TEN); case 2 : num = get_num(MINS); case 3 : num = get_num(MINS_TEN); case 4 : num = get_num(HRS); case 5 : num = get_num(HRS_TEN); } } //Send number to DATA_PORT send_data(DATA_PORT, num); } //end forever loop //end main } int get_num(char req) { switch (req) { case SECS : return sec % 10; case SECS_TEN : if (sec <10 ) { return 0; } return sec / 10; case MINS : return hr % 10; case MINS_TEN : if (min <10 ) { return 0; } return min / 10; case HRS : return hr % 10; case HRS_TEN : if (hr <10 ) { return 0; } return hr / 10; //end of function } } void send_data(char port, int num) { switch(num) { case 0 : DATA[0] = 0; DATA[1]= 0; DATA[2] = 0; DATA[3] = 0; case 1 : DATA[0] = 0; DATA[1]= 0; DATA[2] = 0; DATA[3] = 1; case 2 : DATA[0] = 0; DATA[1]= 0; DATA[2] = 1; DATA[3] = 0; case 3 : DATA[0] = 0; DATA[1]= 0; DATA[2] = 1; DATA[3] = 1; case 4 : DATA[0] = 0; DATA[1]= 1; DATA[2] = 0; DATA[3] = 0; case 5 : DATA[0] = 0; DATA[1]= 1; DATA[2] = 0; DATA[3] = 1; case 6 : DATA[0] = 0; DATA[1]= 1; DATA[2] = 1; DATA[3] = 0; case 7 : DATA[0] = 0; DATA[1]= 1; DATA[2] = 1; DATA[3] = 1; case 8 : DATA[0] = 1; DATA[1]= 0; DATA[2] = 0; DATA[3] = 0; case 9 : DATA[0] = 1; DATA[1]= 0; DATA[2] = 0; DATA[3] = 1; } for (x=0; x <=9; x++) { if (!DATA[x]) { clear_bit(port, DATAPIN[x]) ; } else { set_bit(port, DATAPIN[x]); } } } }
Now what i'm having trouble with is this :
void send_data(char port, int num) { switch(num) { case 0 : DATA[0] = 0; DATA[1]= 0; DATA[2] = 0; DATA[3] = 0; case 1 : DATA[0] = 0; DATA[1]= 0; DATA[2] = 0; DATA[3] = 1; case 2 : DATA[0] = 0; DATA[1]= 0; DATA[2] = 1; DATA[3] = 0; case 3 : DATA[0] = 0; DATA[1]= 0; DATA[2] = 1; DATA[3] = 1; case 4 : DATA[0] = 0; DATA[1]= 1; DATA[2] = 0; DATA[3] = 0; case 5 : DATA[0] = 0; DATA[1]= 1; DATA[2] = 0; DATA[3] = 1; case 6 : DATA[0] = 0; DATA[1]= 1; DATA[2] = 1; DATA[3] = 0; case 7 : DATA[0] = 0; DATA[1]= 1; DATA[2] = 1; DATA[3] = 1; case 8 : DATA[0] = 1; DATA[1]= 0; DATA[2] = 0; DATA[3] = 0; case 9 : DATA[0] = 1; DATA[1]= 0; DATA[2] = 0; DATA[3] = 1; } for (x=0; x <=3; x++) { if (!DATA[x]) { clear_bit(port, DATAPIN[x]) ; } else { set_bit(port, DATAPIN[x]); } } } }
The for loop is reasonably simple. the function works out what the data bits should be by using a switch() command. (maybe theres a better way to do this?)
So i go onto the for() loop. this should count from 0 to 3, working out wether the correspondng data bit should be a one or a zero and setting the pin to that value. Using an array like this won't work (i think?) becasue basically that value needs to be 'PDx' where x is the actual number of the pin i want to control. using an array like this will only allow me to have DATAPIN as 0, 1, 2 or 3.
Please can you inform me if i am thinking along the right lines, and suggest any possible solutions!!!
I am a newbie, just trying to muddle along so any helpful thoughts are greatly appreciated. i've already used alot of info from this site to great effect.
Thanks,
Dave