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

BOB_SANTANA program
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Fri Dec 08, 2006 8:22 am     Reply with quote

I think what Bob_santana was saying was.
this works
Code:
printf("%c ",RX_BUF[x]);


this doesn't
Code:
printf("%c",RX_BUF[x]);


And I was guessing it was because he used a hardware USART and overflowed the TXREG of it.
When I tested it, both worked.
BUT i was using a bit-bang on pin E2. Perhaps you are also, kenedy?
BOB_SANTANA



Joined: 16 Oct 2006
Posts: 110
Location: HOVE, EAST SUSSEX

View user's profile Send private message

PostPosted: Fri Dec 08, 2006 4:45 pm     Reply with quote

hI Everyone here is the program you helped me develop
there isstill room for improvement but it works ok
i am very happy considering that 3 months ago i didn't every think about C not to talk of programming in it.
Please feel free to suggest any improvements that is required as i am alway willing to learn from you

Best Regards
Very Happy

Code:

// Thanks to Everyone at CCS Forum that have help me to produce
//this program.                                                                           
//It was a really good learning experience for me.                         
//Special Mention goes to treitmey who wrote RS232 INTERUPT       
//And really opened my eyes on how to write easy readable code   
//Also to PCM Programmer,Ttelmah e.t.c you are all great             
///////////////////////////////////////////////////////////////////////////////
//The Program is used to control 16 relays connected to the following
//position on the pic16f877
//relay is mapped to   PIN_RD4,PIN_RD5,PIN_RD6,PIN_RD7,PIN_RA3,PIN_RA2,PIN_RA1,PIN_RA0
//PIN_RB0,PIN_RB1,PIN_RB2,PIN_RB3,PIN_RB4,PIN_RB5,PIN_RB6,PIN_RB7
// the program is sent a 3 character address followed by 4 characters from a PC
// which represent a 16bit number if the data is valid
//the relays are updated with the entered values in the data entered
//is convetered to binary and displayed on an LCd
//is not valid an error msg in displayed to PC Screen

#define VER_MINOR 1
#define VER_MAJOR 'S'  //Santana
#include <16F877.h>
#ORG 0x1F00,0x1FFF {} // use boot loader for the 8k 16F877
#device *=16
#fuses xt,nowdt,noprotect,nolvp,put
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS,stream=PC) 
#case
#zero_ram
#include <LCD_File.c> // file for lcd defines

//==============//
#define IDLE_TIMER 40
#bit  CREN =0x18.4
//=== Prototypes ===//
void init(void);
void update_relays(int16 relay);
void check_data(void);
void StartUp_Msg(void);
void  Varify_Address(void);
void Varify_Data (void);
void TransferData(void);
int ConvertData(int Rxd );
void UpdateOutputs(void);
void  Data_Lenght(void);
void PrintAsBinary ( int16 value );
//=== Globals ===//

#define True 1
#define False 0 
#define BUF_SZ 16
#define RXMASK BUF_SZ-1
#define LENGTH_OF_SHORT 16u
int8 RX_BUF[BUF_SZ];//SAMPLE DATA AD1FFFF
int8 rx_indx_i,rx_indx_o;
int16 int_count_tmr2;
int8 Address=0;
int8 DataValid=0;
int8 result[4];
int16 updateLcd;
int16 old_relay_value,relay;

BOOLEAN idle_bus=FALSE;//flag for when usart is idle for 1sec with data ready
int8 ERRORS;
//=== MAIN ===//
void main(void)
{
 // int16 old_relay_value,relay;
  init();
  while(1){
    if(relay!=old_relay_value){
      old_relay_value=relay;
      update_relays(relay);
    }
    if(idle_bus){//bus is idle and data is waiting
      idle_bus=FALSE;
      check_data();
     
    }
  }
}

void init(void){
  setup_adc_ports(NO_ANALOGS);
  setup_adc(ADC_OFF);
  set_tris_a(0x00);set_tris_b(0x00);set_tris_c(0xFF);set_tris_d(0x00);set_tris_e(0x00);
  printf("Ver. %C_%03u.c\n\r",VER_MAJOR,VER_MINOR);

  setup_timer_2(T2_DIV_BY_16, 254,16);
  enable_interrupts(INT_RDA);
  enable_interrupts(GLOBAL);
  lcd_init();
  StartUp_Msg();

}
void update_relays(int16 relay){
  //relay is mapped to   PIN_RD4,PIN_RD5,PIN_RD6,PIN_RD7,PIN_RA3,PIN_RA2,PIN_RA1,PIN_RA0
  //PIN_RB0,PIN_RB1,PIN_RB2,PIN_RB3,PIN_RB4,PIN_RB5,PIN_RB6,PIN_RB7
  if(bit_test(relay,0)) output_high(PIN_D4);//test bit 0
  else output_low(PIN_D4);

  if(bit_test(relay,1)) output_high(PIN_D5);//test bit 1
  else output_low(PIN_D5);

  if(bit_test(relay,2)) output_high(PIN_D6);//test bit 2 ...
  else output_low(PIN_D6);

  if(bit_test(relay,3)) output_high(PIN_D7);//test bit 3 ...
  else output_low(PIN_D7);

  if(bit_test(relay,4)) output_high(PIN_A3);//test bit 4 ...
  else output_low(PIN_A3);

  if(bit_test(relay,5)) output_high(PIN_A2);//test bit 5 ...
  else output_low(PIN_A2);

  if(bit_test(relay,6)) output_high(PIN_A1);//test bit 6 ...
  else output_low(PIN_A1);

  if(bit_test(relay,7)) output_high(PIN_A0);//test bit 7 ...
  else output_low(PIN_A0);

  if(bit_test(relay,8)) output_high(PIN_B0);//test bit 8 ...
  else output_low(PIN_B0);

  if(bit_test(relay,9)) output_high(PIN_B1);//test bit 9 ...
  else output_low(PIN_B1);

  if(bit_test(relay,10)) output_high(PIN_B2);//test bit 10 ...
  else output_low(PIN_B2);

  if(bit_test(relay,11)) output_high(PIN_B3);//test bit 11 ...
  else output_low(PIN_B3);

  if(bit_test(relay,12)) output_high(PIN_B4);//test bit 12 ...
  else output_low(PIN_B4);

  if(bit_test(relay,13)) output_high(PIN_B5);//test bit 13 ...
  else output_low(PIN_B5);

  if(bit_test(relay,14)) output_high(PIN_B6);//test bit 14 ...
  else output_low(PIN_B6);

  if(bit_test(relay,15)) output_high(PIN_B7);//test bit 15 ...
  else output_low(PIN_B7);

//. . . go through all pins
}

 
void check_data(void){
  int8 x;

  printf("Data Received :>");
  for (x=0;x<sizeof(RX_BUF);x++){
       putc(RX_BUF[x]);
  }

  disable_interrupts(INT_RDA);
  Varify_Address();
  Varify_Data();
  if (DataValid && Address)
      {
        TransferData();
        UpdateOutputs();
      }
  rx_indx_i=0;rx_indx_o=0;
  enable_interrupts(INT_RDA);
}
//=======================RXisr============================//
//Takes RX data from a stream and puts it into a buffer via interrupts
#INT_RDA
void RXisr(void)
{
  idle_bus=FALSE;
  set_timer2(0);//reset countdown on each rx char
  int_count_tmr2=IDLE_TIMER;
  enable_interrupts(INT_TIMER2);//when you set a timer, turn on the INT
  while(kbhit(PC)){
    RX_BUF[rx_indx_i]=fgetc(PC);
    rx_indx_i++;
    rx_indx_i&=RXMASK;//index is allways 0 to 15
    if (rx_indx_i==rx_indx_o) // check for buffer overflow
    {
      bit_set(ERRORS,2);//set error flag bit 2 for RXbuffer overflow
    }
  }
}
//========timer 2 ISR===============//
#int_timer2                   // This function is called every time
void timer2_isr() {           // timer 2 overflows (250->0), which is
  if(--int_count_tmr2==0){
    idle_bus=TRUE;//set flag
    int_count_tmr2 = IDLE_TIMER;//reset count
    disable_interrupts(INT_TIMER2);//dont need the timer now
  }
}

void StartUp_Msg(void)
{
 int i;

 
  printf(lcd_putc, "\f"); // Clear the LCD.
    delay_ms(500);

    //Display 1st Default Startup Lcd Message
   //-----------------------------------------------------     
   printf(lcd_putc, "\f Thanks All D Help"); // Clear Screen & Write To Line 1.
   printf(lcd_putc, "\n 16 Port HV Switch"); // Write to "Line 2.
   delay_ms(3000);
   lcd_gotoxy(20, 2);              // Go to End of 2nd Line
   for(i = 0; i < 20; i++)         // Backspace over 2nd line.
      {                            // Note This is put here intentionally
       printf(lcd_putc," \b\b");   // to show how to backspace
       delay_ms(150);
      }               
     lcd_gotoxy(20, 1);         // Go to end of first line.       
 
   for(i = 0; i < 20; i++)      // Backspace over first line.
      {
       printf(lcd_putc," \b\b");
       delay_ms(150);
      }

    //Display 2nd Default Startup Lcd Message
   //------------------------------------------------------
   printf(lcd_putc, "\fPT: 0123456789ABCDEF"); // Clear Screen & Write To Line 1.
   printf(lcd_putc, "\nAD1:0000000000000000"); // Write to "Line 2.
   delay_ms(1000);                             // 1 Seconds Delay in mSeconds
} 



void  Varify_Address(void)
// Checks the the addres is AD1
//you can change the addres to any 3 chars but have
// to remember to change it in the if stament below
{ 
   
 if ((RX_BUF[0]=='A')&& (RX_BUF[1]=='D')&& (RX_BUF[2]=='1'))
            {   
            printf("Addr OK\n\r");
            Address=True;               
            }
 else
            {
             printf("Addr Error\n\r");
             Address=False;               
            }

}

void  Varify_Data(void)

/// This Function checks the 4 character locations RX_BUF[3]
/// to RX_BUF[6]  contains a value within 0 - F

{
  int i,Checker,Result;
 
  Result = 0;
  for (i=3; i<7; i++)
   {
      Checker = RX_BUF[i];
      if (( Checker >=48 && Checker <= 57 )||( Checker >=65 && Checker <= 70 ))
            {
             Result++;
         }
     else
            {
             Result--;
            }
    }
//At the end of this process if the correct data type is entered
//the value of result would be 4 anything less would be
//and error
   if ((Result==4)&&(RX_BUF[7]==13))
   {
      printf("Data OK\n\r");
      DataValid=True;
    }
   else
    {
     printf("Data Error\n\r");
     DataValid=False;
    }
}
//I also check here to make sure that lenght of
//the data is 7 anything less or more would produce
//false in DataValid

///////////////////////////////////////////////////////
// This converts the ascii to hex
//Here is just a simple ascii to hex conversion
//of the four chars
int ConvertData(int Rxd )

{
 
 if((Rxd>=48)&&(Rxd<= 57 ))
    {         
      Rxd=Rxd-48;
    }         
else             
  if((Rxd>=65) &&(Rxd<= 70 ))
    {
     Rxd=Rxd-55;               
    }                                                               
   return (Rxd);
}


void TransferData(void)
{

// Tranfer RX_BUF[3]-RX_BUF[6];
// Converts ASCII Range 0-F

   result[0] = ConvertData(RX_BUF[3]);
   result[1]= ConvertData(RX_BUF[4]);
   result[2] = ConvertData(RX_BUF[5]);
   result[3] = ConvertData(RX_BUF[6]);
 } 

 
void UpdateOutputs(void)
{
 updateLcd = ((int16)result[0]<<12) + ((int16)result[1]<<8) + (result[2]<<4) + (result[3]);
 relay=updateLcd;    // Send the converted data to relay which
                     //updates the outputs to the relays
 PrintAsBinary (updateLcd); //Update LCD Display with binary value
 
}


///////////////////////////////////////////////////////
/// coverts the final value to binary 16bits
void PrintAsBinary ( int16 value )
{   char display[16];
    int i;
    int binaryValue;
    for ( i = 0; i < LENGTH_OF_SHORT; i++ )
    {
        /* Check MSB against 1 */
        if ( 0x8000 == ( value & 0x8000 ) )
        {
            binaryValue = 1;
            display[i]='1';
        }
        else
        {
            binaryValue = 0;
             display[i]='x';
        }
        /* Print the binary value */
        lcd_gotoxy(5+i, 2);  // Go to the5th position
                          //of of line 2
         printf(lcd_putc,"%c",display[i] );
        /* Shift next bit into mask position */
        value = value << 1;
    }
}

_________________
BOB_Santana Smile
Mark Weir



Joined: 11 Sep 2003
Posts: 51
Location: New Zealand

View user's profile Send private message

Questions
PostPosted: Tue Dec 12, 2006 1:27 pm     Reply with quote

Hi Bob,

I have been looking through your code and have a couple of questions. Some of these things might be obvious so forgive me.

1 Why do you have 7 characters in your input stream ie AD1 FFFF, would you not get 16 bits from 2?

2 What happens to the data from the extra chracters?

3 When I enter AD1 FFFF or any combination of 4 characters I get Address Valid but Data invalid so nothing happens on the output pins

I am very interested in this project and would like to understand more.

Cheers

Mark
BOB_SANTANA



Joined: 16 Oct 2006
Posts: 110
Location: HOVE, EAST SUSSEX

View user's profile Send private message

PostPosted: Tue Dec 12, 2006 4:16 pm     Reply with quote

Hi Mark

Happy to know you find the project useful

Quote:

1 Why do you have 7 characters in your input stream ie AD1 FFFF, would you not get 16 bits from 2?



As you can see the buffer
Code:

int8 RX_BUF[BUF_SZ];//SAMPLE DATA AD1FFFF


can hold up to 16 characters at a time but for my application
i really only need 4 characters say FFFF which equates to binary
0b1111 1111 1111 1111
that is what is transfered to the output pins to activate my relays

Quote:

2 What happens to the data from the extra chracters?

3 When I enter AD1 FFFF or any combination of 4 characters I get Address Valid but Data invalid so nothing happens on the output pins


That is what you should expect, AD1 is the address
and there should be no space its should be AD1FFFF so AD10000 should give you 0b0000 0000 0000 000
you can change the address to what ever you want be do remember to change it in the program as well here

Code:

 if ((RX_BUF[0]=='A')&& (RX_BUF[1]=='D')&& (RX_BUF[2]=='1'))




Hope i have been able to help

Regards
_________________
BOB_Santana Smile
BOB_SANTANA



Joined: 16 Oct 2006
Posts: 110
Location: HOVE, EAST SUSSEX

View user's profile Send private message

PostPosted: Wed Dec 13, 2006 11:26 am     Reply with quote

Any suggestion on how i can make the program run quicker would be very much appreciated

Thanks Guys
_________________
BOB_Santana Smile
ckielstra



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

View user's profile Send private message

PostPosted: Wed Dec 13, 2006 11:58 am     Reply with quote

Which part of your program do you consider slow? How fast is it now and how much faster do you want it to be?

For example if the response is slow from the command given on the PC you could increase the baudrate from 9600 baud to 115kbaud.
BOB_SANTANA



Joined: 16 Oct 2006
Posts: 110
Location: HOVE, EAST SUSSEX

View user's profile Send private message

PostPosted: Wed Dec 13, 2006 12:40 pm     Reply with quote

ckielstra

From when i enter the characters on the pc it takes about 4 seconds
for my relays to be updated.
i have not got any delays anywhere.
Question : If i now change to a 20Mhz Xtal in theory should the program not run 5 times faster?

Cheers All Razz
_________________
BOB_Santana Smile
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Wed Dec 13, 2006 2:25 pm     Reply with quote

Have you tried reducing the idle bus time?
Quote:
#define IDLE_TIMER 40


This is different from your hardware to mine because the osc is different.

Adjust this via trial and error. You could also change to a faster timmer2 int to increase the granularity.
BOB_SANTANA



Joined: 16 Oct 2006
Posts: 110
Location: HOVE, EAST SUSSEX

View user's profile Send private message

PostPosted: Wed Dec 13, 2006 4:09 pm     Reply with quote

treitmey

Thanks for the tip
i reduced the IDLE_TIMER to 20 and the responce time is much better
1.5Sec

Regards
_________________
BOB_Santana Smile
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Wed Dec 13, 2006 4:17 pm     Reply with quote

This timer tries to figure out if your done typing or just moving your finger from the "F" key to the "x" key. If you have a PC or other PIC/machine
sending the data in a burst, the time can be reduced to the inter-character timing of the baud you use.

Another way to do this is look for the carrage return at the end of the string.
((but you didn't say there is one (just AD1FFFF)))

For giggles. Try sending the data in a burst from siow.
You could then speed the timmer2 way up and adjust the idle again.
And your relays will change "instantaneously"
BOB_SANTANA



Joined: 16 Oct 2006
Posts: 110
Location: HOVE, EAST SUSSEX

View user's profile Send private message

PostPosted: Wed Dec 13, 2006 5:02 pm     Reply with quote

Thanks again
I do have CR at the end of each Data Burst send hence
the if ((Result==4)&&(RX_BUF[7]==13))

in my data check

Cheers
_________________
BOB_Santana Smile
BOB_SANTANA



Joined: 16 Oct 2006
Posts: 110
Location: HOVE, EAST SUSSEX

View user's profile Send private message

PostPosted: Thu Dec 14, 2006 5:20 am     Reply with quote

Hi treitmey

I have just noticed a problem and need some advice from you.
i was doing some testing and discovered that if i have my pc connected to the unit and switch the power off and on from the mains when my
unit powers up the lcd/ pic don't power up until i press the reset on my pic.
what can you suggest?
_________________
BOB_Santana Smile
BOB_SANTANA



Joined: 16 Oct 2006
Posts: 110
Location: HOVE, EAST SUSSEX

View user's profile Send private message

PostPosted: Thu Dec 14, 2006 5:21 am     Reply with quote

those are my fuse settings

#fuses xt,nowdt,noprotect,nolvp,put
_________________
BOB_Santana Smile
BOB_SANTANA



Joined: 16 Oct 2006
Posts: 110
Location: HOVE, EAST SUSSEX

View user's profile Send private message

PostPosted: Thu Dec 14, 2006 6:26 am     Reply with quote

I Think i have found out the problem but not the cure
The problem is that when i start my unit with the RS232 cable pluged in
and turn ON/OFF from the mains the LCD stays blink but the prgram is running in the backgrond.
so when i send the first set of characters the LCD displays garbage but after sending the second set of characters the Lcd displays the correct values.
So this might be that at start the buffer is empty and there is nothing to display
Its not a power problem so maybe i have to start with a default value for the buffer ?
Here is the programme again below

Code:


#define VER_MINOR 1
#define VER_MAJOR "SANTANA"  //Santana
#include <16F877.h>
#ORG 0x1F00,0x1FFF {} // use boot loader for the 8k 16F877
#device *=16
#fuses xt,nowdt,noprotect,nolvp,put
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS,stream=PC)  // Jumpers: 8 to 11, 7 to 1
#case
#zero_ram
#include <LCD_File.c> // file for lcd defines
#include <string.h>


//==============//
#define IDLE_TIMER 20
#bit  CREN =0x18.4
//=== Prototypes ===//
void init(void);
void update_relays(int16 relay);
void check_data(void);
void StartUp_Msg(void);
void  Varify_Address(void);
void Varify_Data (void);
void TransferData(void);
int ConvertData(int Rxd );
void UpdateOutputs(void);
void  Data_Lenght(void);
void  ClrLcd(void);
void PrintAsBinary ( int16 value );
//=== Globals ===//

#define True 1
#define False 0 
#define BUF_SZ 16
#define RXMASK BUF_SZ-1
#define LENGTH_OF_SHORT 16u
int8 RX_BUF[BUF_SZ];//SAMPLE DATA AD1FFFF
int8 rx_indx_i,rx_indx_o;
int16 int_count_tmr2;
int8 Address;//lenght=0;
int8 DataValid=0;
int8 result[4];
int16 updateLcd;
int16 old_relay_value,relay;

BOOLEAN idle_bus=FALSE;//flag for when usart is idle for 1sec with data ready
int8 ERRORS;
//=== MAIN ===//
void main(void)
{
   
  init();
  while(1){
    if(relay!=old_relay_value){
      old_relay_value=relay;
      update_relays(relay);
    }
    if(idle_bus){//bus is idle and data is waiting
      idle_bus=FALSE;
      check_data();
     
    }
  }
}
void init(void){
  setup_adc_ports(NO_ANALOGS);
  setup_adc(ADC_OFF);
  set_tris_a(0x00);set_tris_b(0x00);set_tris_c(0xFF);set_tris_d(0x00);set_tris_e(0x00);
  output_b(0x00); output_d(0x00); output_a(0x00);
  printf("\fHV Router..%S_%03u.c\n\r",VER_MAJOR,VER_MINOR);
   lcd_init();
  setup_timer_2(T2_DIV_BY_16, 254,16);
  enable_interrupts(INT_RDA);
  enable_interrupts(GLOBAL);
  // lcd_init();
  StartUp_Msg();
  relay=0;
}
void update_relays(int16 relay){
  //relay is mapped to   PIN_RD4,PIN_RD5,PIN_RD6,PIN_RD7,PIN_RA3,PIN_RA2,PIN_RA1,PIN_RA0
  //PIN_RB0,PIN_RB1,PIN_RB2,PIN_RB3,PIN_RB4,PIN_RB5,PIN_RB6,PIN_RB7
  if(bit_test(relay,0)) output_high(PIN_D4);//test bit 0
  else output_low(PIN_D4);

  if(bit_test(relay,1)) output_high(PIN_D5);//test bit 1
  else output_low(PIN_D5);

  if(bit_test(relay,2)) output_high(PIN_D6);//test bit 2 ...
  else output_low(PIN_D6);

  if(bit_test(relay,3)) output_high(PIN_D7);//test bit 3 ...
  else output_low(PIN_D7);

  if(bit_test(relay,4)) output_high(PIN_A3);//test bit 4 ...
  else output_low(PIN_A3);

  if(bit_test(relay,5)) output_high(PIN_A2);//test bit 5 ...
  else output_low(PIN_A2);

  if(bit_test(relay,6)) output_high(PIN_A1);//test bit 6 ...
  else output_low(PIN_A1);

  if(bit_test(relay,7)) output_high(PIN_A0);//test bit 7 ...
  else output_low(PIN_A0);

  if(bit_test(relay,8)) output_high(PIN_B0);//test bit 8 ...
  else output_low(PIN_B0);

  if(bit_test(relay,9)) output_high(PIN_B1);//test bit 9 ...
  else output_low(PIN_B1);

  if(bit_test(relay,10)) output_high(PIN_B2);//test bit 10 ...
  else output_low(PIN_B2);

  if(bit_test(relay,11)) output_high(PIN_B3);//test bit 11 ...
  else output_low(PIN_B3);

  if(bit_test(relay,12)) output_high(PIN_B4);//test bit 12 ...
  else output_low(PIN_B4);

  if(bit_test(relay,13)) output_high(PIN_B5);//test bit 13 ...
  else output_low(PIN_B5);

  if(bit_test(relay,14)) output_high(PIN_B6);//test bit 14 ...
  else output_low(PIN_B6);

  if(bit_test(relay,15)) output_high(PIN_B7);//test bit 15 ...
  else output_low(PIN_B7);

//. . . go through all pins
}
void check_data(void){
  int8 x;

  printf("Data Received :>");
  for (x=0;x<sizeof(RX_BUF);x++){
       putc(RX_BUF[x]);
  }
 // printf("\n\r");
  disable_interrupts(INT_RDA);
  Varify_Address();
  Varify_Data();
  if (DataValid && Address)
      {
        TransferData();
        UpdateOutputs();
      }
  rx_indx_i=0;rx_indx_o=0;
  enable_interrupts(INT_RDA);
}
//=======================RXisr============================//
//Takes RX data from a stream and puts it into a buffer via interrupts
#INT_RDA
void RXisr(void)
{
  idle_bus=FALSE;
  set_timer2(0);//reset countdown on each rx char
  int_count_tmr2=IDLE_TIMER;
  enable_interrupts(INT_TIMER2);//when you set a timer, turn on the INT
  while(kbhit(PC)){
    RX_BUF[rx_indx_i]=fgetc(PC);
    rx_indx_i++;
    rx_indx_i&=RXMASK;//index is allways 0 to 15
    if (rx_indx_i==rx_indx_o) // check for buffer overflow
    {
      bit_set(ERRORS,2);//set error flag bit 2 for RXbuffer overflow
    }
  }
}
//========timer 2 ISR===============//
#int_timer2                   // This function is called every time
void timer2_isr() {           // timer 2 overflows (250->0), which is
  if(--int_count_tmr2==0){
    idle_bus=TRUE;//set flag
    int_count_tmr2 = IDLE_TIMER;//reset count
    disable_interrupts(INT_TIMER2);//dont need the timer now
  }
}

void StartUp_Msg(void)
{
 int i;

 
  printf(lcd_putc, "\f"); // Clear the LCD.
    delay_ms(500);

    //Display 1st Default Startup Lcd Message
   //-----------------------------------------     
 
   printf(lcd_putc, "\f Thanks All D Help");
   printf(lcd_putc, "\n 16 Port HV Switch"); // Write to "Line 2.
   delay_ms(1500);
   lcd_gotoxy(20, 2);              // Go to End of 2nd Line
   for(i = 0; i < 20; i++)         // Backspace over 2nd line.
      {
       printf(lcd_putc," \b\b");
       delay_ms(50);
      }               
     lcd_gotoxy(20, 1);         // Go to end of first line.       
 
   for(i = 0; i < 20; i++)      // Backspace over first line.
      {
       printf(lcd_putc," \b\b");
       delay_ms(50);
      }

    //Display 2nd Default Startup Lcd Message
   //----------------------------------------
   printf(lcd_putc, "\fPT: 0123456789ABCDEF"); // Clear Screen & Write To Line 1.
   printf(lcd_putc, "\nAD1:0000000000000000"); // Write to "Line 2.
   delay_ms(1000);                             // 1 Seconds Delay in mSeconds
} 



void  Varify_Address(void)
// Checks the the addres is AD!
{ 
   
 if ((RX_BUF[0]=='A')&& (RX_BUF[1]=='D')&& (RX_BUF[2]=='1'))
            {   
            printf("Addr OK\n\r");
            Address=True;               
            }
 else
            {
             printf("Addr Error\n\r");
             Address=False;               
            }

}

void  Varify_Data(void)
///////////////////////////////////////////////////////
/// This Function checks the 4 character locations Data[3]to Data[6]
/// Has the values 0 - F

{
  int i,lenght,Checker,Result;
  lenght =0;
  Result = 0;
  for (i=3; i<7; i++)
   {
      Checker = RX_BUF[i];
      if (( Checker >=48 && Checker <= 57 )||( Checker >=65 && Checker <= 70 ))
             {
           
               Result++;
            }
     else
             {
              Result--;
             }
    }

         for (i=0; RX_BUF[i]!=13; i++)
                {
                    lenght=(i+1);
                 }
 
   if ((Result==4)&&(RX_BUF[7]==13)&&(lenght==7) )
             {
             printf("Data OK\n\r");
             DataValid=True;
             
            }
   else
             {
              printf("Data Error\n\r");
              DataValid=False;
             }


 




}


///////////////////////////////////////////////////////
// This converts the ascii to hex
int ConvertData(int Rxd )

{
 
     if((Rxd>=48)&&(Rxd<= 57 ))
             {
               Rxd=Rxd-48;
             }
             else
                 if((Rxd>=65) &&(Rxd<= 70 ))
                   {
                    Rxd=Rxd-55;
                   }
                   
                 
   return (Rxd);
}


void TransferData(void)
{

// Tranfer RX_BUF[3]-RX_BUF[6];
// Converts ASCII Range 0-F

   result[0] = ConvertData(RX_BUF[3]);
   result[1]= ConvertData(RX_BUF[4]);
   result[2] = ConvertData(RX_BUF[5]);
   result[3] = ConvertData(RX_BUF[6]);
 } 

 
void UpdateOutputs(void)
{
 updateLcd = ((int16)result[0]<<12) + ((int16)result[1]<<8) + (result[2]<<4) + (result[3]);
 relay=updateLcd;               // Sets 16 Bits output port
  ClrLcd ();
 PrintAsBinary (updateLcd);     //Update LCD Display with binary value
 
}


///////////////////////////////////////////////////////
/// coverts the final value to binary 16bits
void PrintAsBinary ( int16 value )
{   char display[16];
    int i;
    int binaryValue;

    for ( i = 0; i < LENGTH_OF_SHORT; i++ )
    {
        /* Check MSB against 1 */
        if ( 0x8000 == ( value & 0x8000 ) )
        {
            binaryValue = 1;
            display[i]='1';
        }
        else
        {
            binaryValue = 0;
             display[i]='X';
        }
        /* Print the binary value */
       
        lcd_gotoxy(5+i, 2);  // Go to end of first line.
        printf(lcd_putc,"%c",display[i] );
        printf("%c",display[i] ); //display to pc is ok?
        /* Shift next bit into mask position */
        value = value << 1;
    }
    printf("\n\r");
     
} 


void ClrLcd (void)
{
   int i;
   printf(lcd_putc, "\f"); // Clear the LCD.    
   printf(lcd_putc, "\fPT: 0123456789ABCDEF"); // Clear Screen & Write To Line 1.
   printf(lcd_putc, "\nAD1:0000000000000000"); // Write to "Line 2.
   printf( "\fPT: 0123456789ABCDEF\n\r"); // Clear Screen & Write To Line 1.
   printf("AD1:");
                               // 1 Seconds Delay in mSeconds

 } 


_________________
BOB_Santana Smile
BOB_SANTANA



Joined: 16 Oct 2006
Posts: 110
Location: HOVE, EAST SUSSEX

View user's profile Send private message

solved
PostPosted: Thu Dec 14, 2006 8:45 am     Reply with quote

Hi Guys

I have got it sorted now.
I had to enable brownout in my fuse and its ok now
_________________
BOB_Santana Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
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