CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

help, writing the 2 mode currency converter

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
wei0927



Joined: 21 Nov 2011
Posts: 1
Location: Malaysia

View user's profile Send private message AIM Address MSN Messenger

help, writing the 2 mode currency converter
PostPosted: Mon Nov 21, 2011 10:07 pm     Reply with quote

This is my program, first trying to store the value keyed in by keypad, and then try to convert the number which are entered to another foreign country currency, but the problem is it can't convert..
Code:

//Proteus Design 3//
#include <16F877a.h>
#FUSES hs,noprotect,nowdt,nolvp,NOBROWNOUT,PUT
#use delay(clock=20000000)
#define use_portb_lcd TRUE
#include <lcd.c>
#include <math.h>

#byte porta=5
#byte portb=6
#byte portc=7
#byte portd=8


int p=0;
int a;
int e=0;
int f=a;
int g;
int i=0;
int b;
int d;
int n[16]={10};
int j=0;
int k=0;
int number;
double z=0;
double sum=0;

void main()
{
set_tris_a(0xff);
set_tris_d(0b00001111);
set_tris_b(0x00);
lcd_init();


//lcd_gotoxy(1,1);
//lcd_putc("Welcome to BAnk");


 while (TRUE)
   { 
   if (!input (pin_a0))
{

b=a-1;
a=1;
for (i=0;i<b;i++)
{
z=lcd_getc(a,2);

if(z==48){
p=0;
}
else if (z==49){
p=1;
}
else if (z==50){
p=2;
}
else if (z==51){
p=3;
}
else if (z==52){
p=4;
}
else if (z==53){
p=5;
}
else if (z==54){
p=6;
}
else if (z==55){
p=7;
}
else if (z==56){
p=8;
}
else if (z==57){
p=9;
}
else{
p=100;
}
n[i]=p;
lcd_gotoxy(a,1);
printf(lcd_putc,"%u ",p);
a++;
f=a;
delay_ms(50);

}
a=1;
for(number=f;number>0;number--)
{
 z=pow(10,number-1);
 sum+=z;
 lcd_gotoxy(sum,1);
 printf(lcd_putc,"%u ",sum);
}

else {
       
      output_low(pin_d4);
      output_high(pin_d5);
      output_high(pin_d6);
      delay_ms(50);
     
     
     
     
      // array 1 scan
       
     

         if (!input(pin_d0))  //value=0 Volts
         {
            lcd_gotoxy(a,2);
            printf(lcd_putc,"1 ");
           
            a++;
            delay_ms(50);           
         }
         
         if (!input(pin_d1))
         {
            lcd_gotoxy(a,2);
            printf(lcd_putc,"4 ");       
            a++;
            delay_ms(50);         
         }
         
         if (!input(pin_d2))
         {
            lcd_gotoxy(a,2);
            printf(lcd_putc,"7 ");
            a++;
            delay_ms(50);         
         }
         
         if (!input(pin_d3))
         {
            lcd_gotoxy(a,2);
            a++;
            printf(lcd_putc,"* ");
            delay_ms(50);
         }
         
         
         /////////////////////////////////scan 1 complete
         
      output_high(pin_d4);
      output_low(pin_d5);
      output_high(pin_d6);
      delay_ms(50);
      // array 2 scan

         if (!input(pin_d0))
         {
            lcd_gotoxy(a,2);
            printf(lcd_putc,"2 ");
            a++;
            delay_ms(50);           
         }
         
         if (!input(pin_d1))
         {
            lcd_gotoxy(a,2);
            a++;
            printf(lcd_putc,"5 ");
            delay_ms(50);         
         }
         
         if (!input(pin_d2))
         {
            lcd_gotoxy(a,2);
            a++;
            printf(lcd_putc,"8 ");
            delay_ms(50);         
         }
         
         if (!input(pin_d3))
         {
            lcd_gotoxy(a,2);
            a++;
            printf(lcd_putc,"0 ");
            delay_ms(50);
         }
         
         
         /////////////////////////////////scan 2 complete
         
      output_high(pin_d4);
      output_high(pin_d5);
      output_low(pin_d6);
      delay_ms(50);
      // array 3 scan

         if (!input(pin_d0))
         {
            lcd_gotoxy(a,2);
            printf(lcd_putc,"3 ");
            a++;
            delay_ms(50);           
         }
         
         if (!input(pin_d1))
         {
            lcd_gotoxy(a,2);
            a++;
            printf(lcd_putc,"6 ");
            delay_ms(50);         
         }
         
         if (!input(pin_d2))
         {
            lcd_gotoxy(a,2);
            a++;
            printf(lcd_putc,"9 ");
            delay_ms(50);         
         }
         
         if (!input(pin_d3))
         {
            lcd_gotoxy(a,2);
            a++;
            printf(lcd_putc,"# ");
            delay_ms(50);
         }
     
         /////////////////////////////////scan 3 complete
         
}
if(!input(pin_a1))
{
k=sum*2;
lcd_putc('\f');
delay_us(50);
lcd_gotoxy(1,1);
printf(lcd_putc,"%u",k);
delay_ms(500);
}
if(!input(pin_a2))
{
k=e+2;
lcd_putc('\f');
delay_us(50);
lcd_gotoxy(1,1);
printf(lcd_putc,"%u",k);
delay_ms(500);
}
if(!input(pin_a3))
{
k=e+2;
lcd_putc('\f');
delay_us(50);
lcd_gotoxy(1,1);
printf(lcd_putc,"%u",k);
delay_ms(500);
}
if(!input(pin_a4))
{
k=e+2;
lcd_putc('\f');
delay_us(50);
lcd_gotoxy(1,1);
printf(lcd_putc,"%u",k);
delay_ms(500);
}



}
}
}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Nov 22, 2011 6:53 am     Reply with quote

Wow, there are so many problems with your program that I don't know where to begin. This must be one of the worst program examples I have seen the last 3 years here on this forum.

Programming is about in detail describing to the computer what you want it to do. Because the computer is a very stupid machine and only understands 2 conditions, True or False, you have to be very precise. Your program 'looks' sloppy, and from that alone it is easy to make errors. To minimize the risk of making errors many programmers have added extra rules on top of the grammar required by C:
1) Use correct indentation!!! This makes your program easier to read and to identify errors in loop constructs. Now your indentations jumps around like crazy.

2) Write defined values in capital letters. This distinguishes them clearly from variables.
For example:
Code:
#FUSES hs,noprotect,nowdt,nolvp,NOBROWNOUT,PUT
Here you mix small and capital words where all of these names are defined values.
I know the CCS compiler does not complain, but you make life easier to yourself by sticking to this simple rule. The official C language makes it compulsory to use the same capitals in the definition as in usage.
Try adding #case to the start of your program so the compiler will do some extra checks for you.

3) Functions should be small as it makes your code easier to read and understand. Maximum size for a function is about 40 lines. Your main is too large.

4) Variable names should have a clear name, not a, f, p or z.
The only allowed exception is 'i' as a counter in a simple for-loop.

5) If you write similar sequences of code for 3 or more times, then it should be replaced by a function. Again, this makes your program easier to read and maintain.

The above guidelines are very basic and should be adhered to by every programmer, especially beginning programmers.

Then there are also bugs in your program, for example:
Code:
b=a-1;
At program start up 'a' has a random value, so b will become random too.

I suggest you do a major cleanup / rewrite of your code and then post the new version.
andyfraser



Joined: 04 May 2004
Posts: 47
Location: UK

View user's profile Send private message

help, writing the 2 mode currency converter
PostPosted: Tue Nov 22, 2011 7:21 am     Reply with quote

Also, can you spot the trend ?

z = 48, p = 0
z = 49, p = 1
z = 50, p = 2

How about p = z - 48 ?

Andy
temtronic



Joined: 01 Jul 2010
Posts: 9215
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Nov 22, 2011 9:17 am     Reply with quote

Good news..1st line says it's a simulation, so no hardware will be fried !!
some points...
1) if variables are to only be positive whole numbers, define them as 'char' NOT int. In CCS C , int means -128...+127,char means 0....255. Press F11 to onpen up the CCC onscreen help files.

2) add 'zero ram' at the beginning,otherwise varibles can hold any random value.

3) use the CCS supplied kbd.c 'driver'. You'll get more help from this forum.Most do NOT have the time to try to figure out how or if your kbd 'driver' works.

4)use variable names that 'selfdescribe' WHAT they are or do.Take advantage of this feature. Unlike the first MS BASIC that only allowed 2 character variables !

5) create 'functions' for the various parts of the program.Have 'main' call them in a neat and orderly fashion.

6)if you use some code several times( ie: delay_ms(500)), create a function(ie: delay_half_second() ) that you can use over and over again. It's save memory space and makes for better coding

7) instead of 'pin_a4', rename it to WHAT it does. It could be a row or column of the keypad...but hard for us to figure out.Again, use the kpd.c from the examples....

8) add lots of comments ! From cutting assembler, I would comment EVERY line,even the obvious ones since 1,2 days later or a month, you WILL forget WHAT your code does! (k=e+2)....why ??????
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Nov 22, 2011 2:11 pm     Reply with quote

are there $$ already stranded in this converter ??
LOL - Laughing
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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