|
|
View previous topic :: View next topic |
Author |
Message |
TselelTJ
Joined: 10 Mar 2010 Posts: 3
|
4 Decimal, 7-Segment counter (0-9999) |
Posted: Wed Mar 10, 2010 11:27 am |
|
|
Good Day ppl.
I'm taking my baby steps with ccs code.
May somebody help me with the code for the following:
The cct must count from 0 to 9999 and it uses a switch which stops the count at any moment, when switch closes it starts again at 0.
1. PIC18F2220
2. Switch at RC0.
3. Bus Wire [RB0-RB7] to U1-U4 (74HC373)
4. Four 7-Segment displays (Common Anode)
5. RC1-RC4 LE wires to U1-U4 (74HC373)
6. OE is Grounded (74HC373)
Thanks in advance. |
|
|
TselelTJ
Joined: 10 Mar 2010 Posts: 3
|
|
Posted: Fri Mar 12, 2010 3:26 am |
|
|
I was able to do the ff but now i need help with the switch, If the switch is open all segments must be off and when closed it must start from 0.
Code: |
#include "Counter_7Seg.h"
char map[10]={0xc0,oxf9,0xa4,0xb0,0x99,0x82,0xf8,0x80,0x09};
void main (){
int i, j, k, l;
port_b_pullups(TRUE);
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator (NC_NC_NC);
setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab
i=0;
j=0;
k=0;
l=0;
delay_ms(1000);
while(TRUE)
{
if(input(PIN_C0))
output_b(map[i]);
output_high(PIN_C4);
delay_ms(30);
output_low(PIN_C4);
if(input(PIN_C0))
output_b(map[j]);
output_high(PIN_C3);
delay_ms(30);
output_low(PIN_C3);
if(input(PIN_C0))
output_b(map[k]);
output_high(PIN_C2);
delay_ms(30);
output_low(PIN_C2);
if(input(PIN_C0))
output_b(map[l]);
output_high(PIN_C1);
delay_ms(30);
output_low(PIN_C1);
if(i<10)
{
i++;
}
if(i==10)
{
j++;
i=0;
}
if(j==10)
{
k++;
j=0;
}
if(k==10)
{
l++;
j=0;
}
if(l==10)
{
i=0;
j=0;
k=0;
l=0;}
}
} |
|
|
|
jbmiller
Joined: 07 Oct 2006 Posts: 73 Location: Greensville,Ontario
|
|
Posted: Sun Mar 14, 2010 7:09 am |
|
|
Just a tip on coding...
It'll help if you name variables with what they mean instead of i,j,k,...
Same goes true for I/P pins...tens_digit,ones_digit,....
While you may remember them for the next few days, a week or so from now, you'll be asking, What does this mean !! Been there..did that !
It also helps everyone else trying to help you out !
Also if you need to reassign a pin, it can be done with one line of code at the beginning, instead of looking for all of them in a few hundred lines of code!
Hope this helps
Jay |
|
|
TselelTJ
Joined: 10 Mar 2010 Posts: 3
|
|
Posted: Thu Mar 18, 2010 8:17 am |
|
|
Here is the working code, I finally got it right...
Code: | #include "Assignment.h"
char map[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void main(){
int8 DD1=0, DD2=0, DD3=0, DD4=0;
port_b_pullups(TRUE);
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
output_high(PIN_C4);
output_high(PIN_C3);
output_high(PIN_C2);
output_high(PIN_C1);
while(TRUE)
{
if(input(PIN_C0))
{
output_b(0xff);
output_b(map[DD1]);
output_high(PIN_C4);
delay_ms(30);
output_low(PIN_C4);
output_b(map[DD2]);
output_high(PIN_C3);
delay_ms(30);
output_low(PIN_C3);
output_b(map[DD3]);
output_high(PIN_C2);
delay_ms(30);
output_low(PIN_C2);
output_b(map[DD4]);
output_high(PIN_C1);
delay_ms(30);
output_low(PIN_C1);
if(DD1<10)
{
DD1++;
}
if(DD1==10)
{
DD1=0;
DD2++;
}
if(DD2==10)
{
DD2=0;
DD3++;
}
if(DD3==10)
{
DD3=0;
DD4++;
}
if(DD4==10)
DD4=0;
}
else
{
output_high(PIN_C4);
output_high(PIN_C3);
output_high(PIN_C2);
output_high(PIN_C1);
output_b(0xff);
DD1=0;
DD2=0;
DD3=0;
DD4=0;
}
}
} |
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|