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 in coding

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



Joined: 07 Mar 2009
Posts: 12

View user's profile Send private message

Help in coding
PostPosted: Tue Mar 10, 2009 7:58 pm     Reply with quote

My coding idea is that there are four inputs(indicate by row1, row2 and column1, column2). The input is in matrix form:
eg: column1 column2
row1 1 2
row2 3 4

If using case statement, there are a lot of possibilities, the coding will be very long because there are possibilities that position 2 and 4 giving high input for row and column or position 2,3,4 giving high input for row and column and a lot more condition.

So, i think whether i can do like 2 loops, first reading the input of row1 followed column1, then column2, if both are high (eg: row1 and column1 both high), the output is high, repeated the process for check row2 followed by column1 and column2.
What i write is as below:

Code:

if (input(pin_B0)){
    if (input(pin_B2)){
        output_high(pin_C0)
        }
    else {
          output_high(pin_C0)
           }
     }
else {
       if (input(pin_B3)){
          output_high(pin_C0)
          }
      else (input(pin_B3)){
             output_high(pin_C0)
          }
       }


So, can i write the coding like this as i want to simplify the coding? Waiting for reply...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 10, 2009 9:15 pm     Reply with quote

Read the 4 bits. Combine them into the lower 4 bits of one byte.
Then use a switch-case statement to take action based upon the 16
possible values.
aliciamee



Joined: 07 Mar 2009
Posts: 12

View user's profile Send private message

PostPosted: Fri Mar 13, 2009 6:52 pm     Reply with quote

PCM programmer wrote:
Read the 4 bits. Combine them into the lower 4 bits of one byte.


Sorry, i don't understand what do u mean... Can u explain more?

Thanks for your reply!!!
arunb



Joined: 08 Sep 2003
Posts: 492
Location: India

View user's profile Send private message Send e-mail

RE:
PostPosted: Sat Mar 14, 2009 12:33 am     Reply with quote

Assume the following status of the rows

Row 1 1 2
Status 1 0

Row 2 3 4
Status 0 1

In row 1, 1 & 2 are in high and low states respectively, while in row 2 the inputs are in low and high states respectively.

To make things easier combine these states like this...( row 1 = 1 0 and row 2 = 0 1)

After combination we get...

1 0 0 1 (0x09 in Hex)

Put this in a register, and then use the register (assume nComb) in the switch statement to get the different combinations.

Code:

Switch (nComb)
{
    Case 1:
   
    break;

}



Hope this helps..

thanks
aa
aliciamee



Joined: 07 Mar 2009
Posts: 12

View user's profile Send private message

PostPosted: Sat Mar 14, 2009 7:25 pm     Reply with quote

Mean that i can write the coding like this?:

Code:
int a;
a==input_b();

switch (a)
{
  case 1:
          output_low(pin_C0);
          break;
  case 2:
          output_low(pin_C0);
          break;
  case 3:
          output_high(pin_C0);
          break;
    .
    .
    .
  case default
}


Actually, my whole project needs to detect the infrared signal (digital input data) from circuit and if [row1, column1] = high, it will send data to the pc via USB and then monitoring the data to match the corresponding position with the database and send out email to the user.
Eg: four box corresponding to 4 users, box1 with position [row1, column1] is in document presence condition (high digital input), then after match with database will send to the email of user1.

I have work out the project based on 1 project i find from internet. They using MC68HC12 which the assembly program will loads a binary number into its memory and transmits the number over serial port to the server. Each pin represents is own binary number, port P of HC12 is used.

Pin : 0,1,2,3,4,5,6,7
Binary Equivalent : 1,2,4,8,16,32,64,128

How I going to know the binary equivalent of the pin? I use either using PIC18F2550 or PIC18F4550, do they have binary equivalent for the pin?

I write the code based on the idea that if the input is different from 0, it will send data to PC and the pin got binary equivalent. But I not so sure that what I write will send data to PC or not. Because I quite confuse about the command USB_cdc_putc, as it is sending character... Plz help me look through this code and give me some comments.
Code:
void main (){
     USB_cdc-init ();
     usb_init ();
     char c;

     while (!usb_cdc_connected ()) {}
 
     do{
        usb_task ();
          if (port_b_pullsups(false)){
              if (usb_enumerated ()){
                  if (kbhit ()) {
                      c = getc();
                       if (c=='\n')(usb_cdc_putc('\r');usb_cdc_putc('/n');)
                       if (c=='\r')(usb_cdc_putc('\r');usb_cdc_putc('/n');)
                       else (usb_cdc_putc(c);)
                     }

                  if (usb_cdc_kbhit ()) {
                      c = usb_cdc_getc();
                       if (c=='\n')(putc('\r');putc('/n');)
                       if (c=='\r')(putc('\r');putc('/n');)
                       else (putc(c);)
                     }
                  }
               }
          }



The following is the small snippet of the com port monitoring C++ program that I get from the project I found. This is the part that takes the signals received from the microcontroller and executes the mySQL queried through the use of the API:
Code:
LRESULT CCommspyDlg::OnRX(WPARAM wLen, LPARAM hBuf)
{
   LPBYTE lpBuf = (LPBYTE)::GlobalLock((HGLOBAL)hBuf);
   if(!lpBuf)
      return 1;

   CString strOutput;

   if(m_settings.nFormat == IDC_ASCII)
   {
      strOutput = lpBuf;
   }
   else
   {
      CString strCvt;

      for(int i = 0; i < (int)wLen; i++)
      {
         switch(m_settings.nFormat)
         {
            case IDC_DECIMAL:
               strCvt.Format("%3d ", (BYTE)lpBuf[i]);
               break;
            case IDC_OCTAL:
               strCvt.Format("%3o ", (BYTE)lpBuf[i]);
               break;
            // hex
            default:
               strCvt.Format("%02x ", (BYTE)lpBuf[i]);
               break;
         }

         strOutput += strCvt;
             //strOutput = strCvt;
             int box = atoi(strCvt);
              try {
                  Connection con("amns");
                  Query query = con.query();
                  switch(box)
                  {
                        case 1: query << "UPDATE contact SET has_mail = '1' WHERE box = '1'"; break;

                        case 2: query << "UPDATE contact SET has_mail = '0', been_notified = '0' WHERE box = '1'"; break;

                        case 4: query << "UPDATE contact SET has_mail = '1' WHERE box = '2'"; break;

                        case 8: query << "UPDATE contact SET has_mail = '0', been_notified = '0' WHERE box = '2'"; break;

                        case 16: query << "UPDATE contact SET has_mail = '1' WHERE box = '3'"; break;

                        case 32: query << "UPDATE contact SET has_mail = '0', been_notified = '0' WHERE box = '3'"; break;

                        case 64: query << "UPDATE contact SET has_mail = '1' WHERE box = '4'"; break;

                        case 128: query << "UPDATE contact SET has_mail = '0', been_notified = '0' WHERE box = '4'"; break;

                        case 3: query << "UPDATE contact SET has_mail = '1' WHERE box = '5'"; break;

                        case 12: query << "UPDATE contact SET has_mail = '0', been_notified = '0' WHERE box = '5'"; break;

                        case 48: query << "UPDATE contact SET has_mail = '1' WHERE box = '6'"; break;

                        case 192: query << "UPDATE contact SET has_mail = '0', been_notified = '0' WHERE box = '6'"; break;

                        default:break;

                  }
                  Result res = query.store();
                  }
              catch (BadQuery er){ // handle any connection
                                   // or query errors that may come up
                  cerr << "Error: " << er.error << endl;
                  return -1;
               
              }

              catch (BadConversion er) {
                  // we still need to cache bad conversions incase
                  // something goes
                  // wrong when the data is converted into stock
                  cerr << "Error: Tried to convert \"" << er.data << "\" to a \""
                   << er.type_name << "\"." << endl;
                  return -1;
              }
      }
   }

   strOutput += "\r\n";

   m_display.SetSel(0,0);
   m_display.ReplaceSel(strOutput);
   m_display.SetSel(0,0);

   ::GlobalUnlock((HGLOBAL)hBuf);
   return 1;
}


But the snippet can only be used with each pin represent its own binary number. So, if I want to refer to this snippet, I have to know about the binary equivalent of the pins, do pins of pic18f2550 or pic18f4550 got binary number? Which port is more suitable to be the input for receiving infrared signal? I have refer to the datasheet but they all seem alike to me.

Where I can find information for the above C++ API? I want to know how it works and how to run it?

Since I used the driver of usb_cdc.h that mean I can monitor the data using com port monitor like what the project does, right? The project using commpspy to monitor the data. The above snippet will later match the mysql(database) to send email(php script). I got find the driver of SMTP.H, but how to use it? I need more eg to understand it so I still stick to the php script and database that I write. Using the driver will it ease my work? Coz according to the project I found, after determine the C++ program received signals from the switches, testing mySQL queries using mySQL API were done by sending data received by the serial monitor to a temporary mySQL database. With the communication established between the switches, com port and database, web application is done by interface with the database. The notification was changed instantly upon data being seen on the com port. So,... if I used the smtp.h driver, will it ease my work?

Sorry for asking so many question, but I actually newbie to all of those that I have work on and it is a short period for me to get the things done. Therefore, answers and opinions will be please and I will very appreciate with all the helps. Thanks!!!
arunb



Joined: 08 Sep 2003
Posts: 492
Location: India

View user's profile Send private message Send e-mail

RE:
PostPosted: Sun Mar 15, 2009 12:52 am     Reply with quote

Quote:

How I going to know the binary equivalent of the pin? I use either using PIC18F2550 or PIC18F4550, do they have binary equivalent for the pin?


All data in registers or ports can be represented in binary form, no conversion is required for this.

Also do not use a==input_b(), use a=input_b() instead.
a will contain a value that represents the status of the pins in port b.

You can send this data over the serial port to the PC, using putc().

thanks
aa
aliciamee



Joined: 07 Mar 2009
Posts: 12

View user's profile Send private message

PostPosted: Sun Mar 15, 2009 7:28 pm     Reply with quote

Oh, is like that how I know the binary form of the pin? Is it the same with the HC12? I need to know it so that I only need to send the data to the PC via USB port, and specify the data from the API like the code I put previously.

Quote:
You can send this data over the serial port to the PC, using putc().


But I am sending via USB port, so can I still use putc()? plz comment on the code I write about the USB, thanks!!! I need to know my coding works or not urgently, what program can i done simulation?

Thanks for all the reply!!! Hope to be reply...
aliciamee



Joined: 07 Mar 2009
Posts: 12

View user's profile Send private message

PostPosted: Tue Mar 17, 2009 4:56 am     Reply with quote

Code:
 
SC0SR   EQU      $00CC
SC0DR   EQU      $00CF
        ORG      $8000

MAIN    LDAA    #00
        LDAA    $0258
        CMPA    #00
        BEQ     MAIN

SEND    STAA   SC0DR
        BRCLR  SC0SR, $80,   
        JMP    MAIN


The explaination for above code is:
The MAIN loop runs and checks the condition of port P, located at memory address $0258. Whenever the memory location it is set to a number different than 0, the program branches to the SEND routine, which sets the serial data direction register, and sends the data via the serial port, which in turn jumps back into the MAIN loop and continue to monitor.

I want to do the similar things but i still really doubt how it loads binary number into its memory and transmits through serial port. If it is the pin own binary number, how could i write in C? Because like what i have write that i post previously, i don't expexted it to be binary form for the data transmission. Comments will be really appreciate!!! I really need a hint. Thanks!!!
aliciamee



Joined: 07 Mar 2009
Posts: 12

View user's profile Send private message

PostPosted: Tue Mar 17, 2009 8:52 pm     Reply with quote

I saw this command in help file:
Code:
int16 i=PIN_B1;

Why there is int16 in front? Is it mean reading value from pin_b1 and i equal to that value?
arunb



Joined: 08 Sep 2003
Posts: 492
Location: India

View user's profile Send private message Send e-mail

RE:
PostPosted: Wed Mar 18, 2009 5:48 am     Reply with quote

putc() will not work with USB, use serial communication instead, its easier to test and debug. When you have tested the code, you can then upgrade to USB.

thanks
a
aliciamee



Joined: 07 Mar 2009
Posts: 12

View user's profile Send private message

PostPosted: Wed Mar 18, 2009 7:17 pm     Reply with quote

Quote:
putc() will not work with USB, use serial communication instead, its easier to test and debug. When you have tested the code, you can then upgrade to USB.


what do u mean by serial communication, is it usb_puts or usb_cdc_putc(c)? I very confuse with this few functions, for eg: usb_cdc_putc(c) sends a character by putting the character into the transmit buffer. Character here can be represented in integer, binary, ascii?

And what do u mean by upgrade to USB?

Quote:
All data in registers or ports can be represented in binary form, no conversion is required for this.

Also do not use a==input_b(), use a=input_b() instead.
a will contain a value that represents the status of the pins in port b.

You can send this data over the serial port to the PC, using putc().


Regarding to above qoute, i understand about the data in the ports can be in binary form without any conversion, but u say that it is returning value of the status of the pins in port, status here mean what? For my understanding, is it the value = 00000011 if input_b() = 00000011?

Could i connect to mysql and send email using the PIC C language, if it is possible i think will ease my works? Coz i have any two weeks to complete it...

Can u reply me as soon as u see this reply? Thanks!!!

A lot of thanks to u, arunb!!!
arunb



Joined: 08 Sep 2003
Posts: 492
Location: India

View user's profile Send private message Send e-mail

RE:
PostPosted: Thu Mar 19, 2009 6:01 am     Reply with quote

Okay here is an example.

Say the following pin states exist on portb

(Here High=1, Low=0)

pin_b0=High
pin_b1=High
pin_b2=Low
pin_b3=Low
pin_b4=Low
pin_b5=Low
pin_b6=Low
pin_b7=Low

if you put a=input_b(), then 'a' contains 3 in decimal or 11 in binary, (since 3 converts to 11 in binary).

Use usb_cdc_putc(a) to send the contents of 'a' as an ASCII character. This means that at the PC end, you will get a character (unprintable), that has an ASCII code of 3.

In VB the Asc() function is used to retrieve the ASCII code of a string. So in the above example, say the code is in variable cRcv.

So when n=Asc(cRcv), n will contain 3.

See documentation for C++ for equivalent functions.

I am not proficient in C++.

thanks
aa
aliciamee



Joined: 07 Mar 2009
Posts: 12

View user's profile Send private message

PostPosted: Fri Mar 20, 2009 11:37 pm     Reply with quote

Ok. From the help file,
usb_cdc_getc() : reads and return a character from the receive buffer
usb_cdc_putc() : put a character into the transmit buffer

The receive buffer and transmit buffer mention is actually the same?

Since i get the data from the circuit, i want to ask will the signal or data will automatically go to the buffer as the USB connect?

I have write the following code using CCS C compiler but i haven't test with the circuit as i can't do simulation, don't which program can done the simulation to see the result. Can u help me go through the code to see whether it will give out my required result?
Code:
#include "C:\Program Files\PICC\Projects\test3.h"
#include <usb_bootloader.h>
#include <usb_cdc.h>


void main()
{
   char signal;
   
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   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);

   usb_cdc_init ();
   usb_init ();                                    //USB is initialized and hoped it is enumerated by the PIC
   
   while (!usb_cdc_connected ()){}                 //see whether it is connected
   
   do {                                            //main cycle
      usb_task ();
      if (usb_enumerated ()){                      //if PIC has been enumerated by the PIC
         if (usb_kbhit (1)){                       //if there is a data in the buffer of entrance USB
            signal = usb_cdc_getc();               //get data from buffer and put in 'signal'
            if (signal > 0){                       //if data is character than differ from zero
               usb_cdc_putc(signal);}              //send the data to transmit buffer, thus to PC
         }
      }
   }while(1);
}


i have compile it, it have no error but warning as below:
>>>warning 203:"C:\Program Files\PICC\drivers\pic18_usb.h"LIne 523(1,1):Condition always TRUE
>>>warning 203"test3.c" Line 35(1,1): Condition always TRUE
>>>warning 216"test3.c" Line 39(0,1): Interrupts disabled during call to prevent re-entrancy: (usb_token_reset)
>>>warning 216"test3.c" Line 39(0,1): Interrupts disabled during call to prevent re-entrancy: (usb_flush_out)
>>>warning 216"test3.c" Line 39(0,1): Interrupts disabled during call to prevent re-entrancy: (usb_cdc_flush_out_buffer)
>>>warning 202:"C:\Program Files\PICC\drivers\usb_desc_cdc.h" Line 154(16,37): Variable never used: USB_CLASS_DESCRIPTORS

memory usage: ROM=12% RAm=56%-58%

it is the warning i get when i change the #include <usb.h> to #include <usb_bootloader.h>, if i use #include <usb.h>, there is even more warnings. So, can i use usb_bootloader.h instead of usb.h? What is the different between them?

From the above coding, do i need to specify the 'signal' is from which port?

i going to use a program called commspy, it is a serial port monitor, it need to specify com port to run the monitor, if i write my code as above, can i use it to monitor my input(data)? Plz answer this answer, it is important to me. And i will retrieve the ascii data by changing a small snippet in the implementation file of commspy.

Reply me asap, thanks!!!
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