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 support@ccsinfo.com

Code works - but did I code it ok?

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



Joined: 18 Jan 2011
Posts: 13

View user's profile Send private message

Code works - but did I code it ok?
PostPosted: Fri Feb 11, 2011 6:51 pm     Reply with quote

Hi,

This code works, but I was wondering if it is optimal code. It is designed to either read rfid tags and print the track name on the screen, or generate random numbers and print the track name on the screen, depending on the position of the switch.

Code:
#include <16F88.H>
#include <string.h>
#define RAND_MAX 10
#include <STDLIB.H>

#fuses INTRC_IO, NOBROWNOUT, NOWDT, NOPROTECT, NOPUT, NOLVP
#use delay(clock=8M)
#use rs232(baud=9600,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8,stream=ID12, ERRORS)

//Define song with temperature range
#define LCD_DB4   PIN_A6
#define LCD_DB5   PIN_A7
#define LCD_DB6   PIN_A0
#define LCD_DB7   PIN_A1
#define LCD_E     PIN_A2
#define LCD_RS    PIN_A3
#define LCD_RW    PIN_A4
#define RFID_START_BYTE 0x02

#include "flex_lcd2.h"

#define MODE_PIN  PIN_B4

int R_Number = 0;

unsigned char s[12];

//Global Structure Type Definition
typedef struct{                                                                                                                 //Define new Structure Type
unsigned char Name[11];                                                                             //Use upto 30 characters for name string
unsigned char ID[11];                                                                                     //Upto 30 chars for ID
unsigned int temp;                                                                                                          //Temp
unsigned int light;                                                                                            //Light
}Track;                                                                                                                                  //Call this structure type: Track


//Global Track Type Variable Definition
const Track MyTrack[10]= {
   {"BoogieWoo1","4200443DF8",0,25},      //1
   {"SoulfulMe2","23o3k3o43p",0,45},      //2
   {"BoogieWoo3","98e9f8d9f8",0,25},      //3
   {"SoulfulMe4","3o3k3o43p3",0,45},      //4
   {"BoogieWoo5","98e9f8d9f8",0,45},      //5
   {"BoogieWoo6","233k3o43p3",0,25},      //6
   {"SoulfulMe7","98e9f8d9f8",0,25},      //7
   {"SoulfulMe8","2o3k3o43p3",0,45},      //8
   {"SoulfulMe9","23o3k3o43p",0,45},      //9
   {"SoulfulM10","23o3k3o43p",0,45}       //10
}; //Create 2 Track structures under MyTrack – extend this to add more tracks


unsigned char get_rfid_string2()
{
   unsigned char len, c;   
   len=0;
   do
   {
      if(!input(MODE_PIN) )  //check MODE - RAND
         return 0;
      if(kbhit(ID12)) //check whther there is any recieved data or not
         c = fgetc(ID12);
   }while (c != RFID_START_BYTE);

   while(len < 10)
   {
      if(!input(MODE_PIN) )  //check MODE - RAND
         return 0;
      if(kbhit(ID12)) //check whther there is any recieved data or not   
         s[len++] = fgetc(ID12);
   }
   s[len] = '\0';
   return len;
}

int get_rand_number()
{
   return rand() % RAND_MAX;
}
void user_delay_s(unsigned int sec_val,unsigned char wait_mode)
{
   unsigned int i;
   for(i = 0; i < sec_val*10; i++)
   {
      delay_ms(100);   
     
      //break if mode change from low to high or high to low
      //it will make more responsive on switch
      if( input(MODE_PIN) != wait_mode) 
      {
         break;
      }
   }
}
void main(){

   signed int temp = 0;
   unsigned char i= 0;
   unsigned char temp_buffer[20];
   unsigned char id_len = 0;
   lcd_init();
   lcd_putc("\f");            //Clear screen
   lcd_putc("Ready");         //Print "Ready"
   
   while(1)
   {
      if( input(MODE_PIN) )      //check MODE (RFID)
      {
         lcd_putc("\fReady");
         //Get RFID string and print on LCD
         id_len = get_rfid_string2();
         if(id_len > 0) //check card preset complete or abort on mode change
         {
            printf(lcd_putc, "\f%s", s);
            printf(lcd_putc, " : %d", id_len);

            temp = -1;
            for(i = 0; i < MAX_INDEX; i++)
            {
               strcpy(temp_buffer,MyTrack[i].ID);  //get string from rom to ram
               temp_buffer[10] = '\0';
               
               if( strcmp(s,temp_buffer) == 0) //compare match and breat on match
               {
                  temp = i;
                  break;
               }
            }           
            //if matched. shows on lcd
            if(temp > -1) printf(lcd_putc, "\n%s", MyTrack[temp].Name);
            delay_ms(2000);
           
            lcd_putc("\fReady");
         }
      }
   
      if(! input(MODE_PIN) )  //check MODE - RAND
      {
     
         //output_toggle(PIN_B7); for debugging
         R_Number = get_rand_number();
         

         //LCD Display
         printf(lcd_putc, "\f%s",MyTrack[R_Number].Name);
       
         //for Serial Port
          //Track<randomnumber>
         printf("\nTrack%d", R_Number);
         //2 second interval
         delay_ms(2000);
      }
     
   }
}


I'd really appreciate some feedback.

James
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sat Feb 12, 2011 6:33 am     Reply with quote

RS232 can be very flaky when relying on a loop in main to catch every inbound char. Sure it can work but small increases in the code in main can easily throw the timing off. The use of the hardware rx pin with a circular buffer fed by an isr is probably the only way to get robust results.
Chantry



Joined: 18 Jan 2011
Posts: 13

View user's profile Send private message

PostPosted: Sat Feb 12, 2011 12:21 pm     Reply with quote

Are there any examples online of this kind of system in CCS C?
temtronic



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

View user's profile Send private message

PostPosted: Sat Feb 12, 2011 12:43 pm     Reply with quote

check in the examples folder , ex_sisr.c (?) memory is fading with age.....
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