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

74595 problem
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

74595 problem
PostPosted: Sun Mar 03, 2019 9:30 pm     Reply with quote

HI

I'm trying to send a byte number, for example if INPUT: 08, all output pins on 74595 ic will become HI. My code can compile, but no output at ic 74595 output when I input any number. Please help me how to solve this problem.

Thanks in advance.
Code:

#include <18F4550.h>
#fuses HSPLL,NOWDT,PROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>
#include <74595.c>

void main()
   {
      BYTE data;
      char c;
      unsigned char d,key;

   usb_init_cs();
   
   while (TRUE)
         {
         usb_task();


            if (usb_cdc_kbhit())
               {
                 c=usb_cdc_getc();
                 if (c=='\n') {putc('\r'); putc('\n');}
                 if (c=='\r') {putc('\r'); putc('\n');}
                                       
                 while(true)
                 
                     {
                  ART:

                  d = usb_cdc_getc();

                         if(d=='R') // push R for RELAY control
                             {
                                while (key!=32) // push SPACE BAR to stop
                                   {

                                       printf(usb_cdc_putc,"\r\nINPUT: ");

                                       data =gethex_usb();

                                       printf(usb_cdc_putc,"\n\r");

                                           
                                           
                                           
                                       write_expanded_outputs (&data);
                                       

                                   } key=0;
                             }

 

                     }
               }       
         }             
}                     
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 03, 2019 10:23 pm     Reply with quote

art wrote:
Please help me how to solve this problem.

Troubleshoot it. Inspect intermediate program values. Add some
debugging code to do this. Example:
Code:

data =gethex_usb();

printf("Data from USB = %x \n\r", data);  // Add this line

Connect the RS232 output from your PIC board to your PC or laptop.
Run Teraterm or Putty, or some terminal program that you like and
see if you are really receiving what you expect from the USB.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Mar 04, 2019 12:33 am     Reply with quote

OK, i've change it to :

Code:

data =gethex1_usb();


if i input 3, data will show 03, but it still no output from 74595.

I've checked &data, it is always 86, what does it mean?
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Mon Mar 04, 2019 1:00 am     Reply with quote

&data, will always be a fixed value. It is the _address_ where 'data' is stored.
It is what is stored at address '86', that is 'data'. This should be 3, when
you input '3'.

You need to now debug your output to the 74595. Send a couple of numbers
like '1', and '2' at an interval. Do they output?. If not you have a hardware
problem with the 74595 connections.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Mar 04, 2019 2:43 am     Reply with quote

After I modified 74595.c file base on my hardware, it works but can only accept input number 0 until F, because of data format is BYTE.
I need to input FF, so all 8 output of 74595 ic will become HIGH.

What should I modified?
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Mon Mar 04, 2019 2:54 am     Reply with quote

Nothing. A byte is 8bits. The standard driver handles all 8bits.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 04, 2019 4:35 am     Reply with quote

He said he modified the driver from this:
Quote:
data =gethex_usb();
to this:
Quote:
OK, i've change it to :

data =gethex1_usb();

gethex1 only gets 4 bits.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Mon Mar 04, 2019 5:05 am     Reply with quote

Aaargh....

Not the driver, but the code feeding the data into it.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Mar 04, 2019 6:15 pm     Reply with quote

OK, i've change the code, and it works. When i send FF, all output 74595 will become HIGH. Next I change the code from input HEX number to input Decimel number. it also can work, when I send 255, all output 74595 will become HIGH. If I want to send decimel 65535 and make all 16 output from 2pcs 74595 ic, do I need to modified the driver ?


Code:

#include <18F4550.h>
#fuses HSPLL,NOWDT,PROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>
#include <74595.c>

void main()
   {
      BYTE data;
      char c;
      unsigned char d,key;

   usb_init_cs();
   
   while (TRUE)
         {
         usb_task();


            if (usb_cdc_kbhit())
               {
                 c=usb_cdc_getc();
                 if (c=='\n') {putc('\r'); putc('\n');}
                 if (c=='\r') {putc('\r'); putc('\n');}
                                       
                 while(true)
                 
                     {
                  ART:

                  d = usb_cdc_getc();

                         if(d=='R') // push R for RELAY control
                             {
                                while (key!=32) // push SPACE BAR to stop
                                   {

                                       printf(usb_cdc_putc,"\r\nINPUT: ");

                                       data =gethex_usb();

                                       printf(usb_cdc_putc,"\r\n %x \n\r");

                                           
                                           
                                           
                                       write_expanded_outputs (&data);
                                       

                                   } key=0;
                             }

 

                     }
               }       
         }             
}



Code:

#include <18F4550.h>
#fuses HSPLL,NOWDT,PROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>
#include <74595.c>

void main()
   {
     
      int8 ar;
      char c;
      unsigned char d,key;

   usb_init_cs();
   
   while (TRUE)
         {
         usb_task();


            if (usb_cdc_kbhit())
               {
                 c=usb_cdc_getc();
                 if (c=='\n') {putc('\r'); putc('\n');}
                 if (c=='\r') {putc('\r'); putc('\n');}
                                       
                 while(true)
                 
                     {
                  ART:

                  d = usb_cdc_getc();

                         if(d=='R') // push R for RELAY control
                             {
                                while (key!=32) // push SPACE BAR to stop
                                   {

                                       get_string_usb(ar,6);

                                       ar =get_int_usb();

                                                                                 
                                           
                                       write_expanded_outputs (&ar);
                                       

                                   } key=0;
                             }

 

                     }
               }       
         }             
}
 
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Mar 04, 2019 11:34 pm     Reply with quote

I already figure it out. Thank you for helping me.

Finally my code :

Code:



#include <18F4550.h>
#fuses HSPLL,NOWDT,PROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>
#include <74595.c>

void main()
   {
     
      int16 ar;
      char c;
      unsigned char d,key;

   usb_init_cs();
   
   while (TRUE)
         {
         usb_task();


            if (usb_cdc_kbhit())
               {
                 c=usb_cdc_getc();
                 if (c=='\n') {putc('\r'); putc('\n');}
                 if (c=='\r') {putc('\r'); putc('\n');}
                                       
                 while(true)
                 
                     {
                  ART:

                  d = usb_cdc_getc();

                         if(d=='R') // push R for RELAY control
                             {
                                while (key!=32) // push SPACE BAR to stop
                                   {                                   
             
                                       ar =get_long_usb();
   
                                       write_expanded_outputs (&ar);
                                       
                                   } key=0;
                             }

 

                     }
               }       
         }             
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Tue Mar 05, 2019 1:28 am     Reply with quote

Your original code with gethex, would have worked provided you were
happy to type two hex digits.
You changed this to gethex1,, which can only get one digit. Which is
why you could only access 4 bits.
You are now unnecessarily using getlong, which can get a 16bit value
though you are only outputting an int8. Simple getint will give smaller
code and still give you the 8 bits.
Using getint, or getlong implies also you are now having to put the number
in decimal not hex. Question of which is easier/better?.

You need to start understanding 'sizes'. int8 = 8 bits.
'long'= int16 = 16bits.
One hex digit = 4bits.
temtronic



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

View user's profile Send private message

PostPosted: Tue Mar 05, 2019 6:21 am     Reply with quote

just saw this.....
unsigned char d,key;

In CCS C, 'char' is an unsigned 8 bit variable, so you don't need to type 'unsigned' in front of it. It should save you a lot of typing. Since I can't touchtype ANY 'shortcut' is welcomed!
To me a 'char' is 0....255 ( always a positive number).

CCS also has BYTE ( it's in the device header), but as an int8. That I think of as any number from -128..+127.
The 'fun' begins when you switch to a different series PIC. The bigger PICS are 16bit based, not 8 bit. And if you come from the PC world,they're diffrent too.

so... it's always a GREAT idea to declare variables and see what the 'default' settings are..

This is all probably documented 'somewhere' that we never read it
Jay
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

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

Stdint
PostPosted: Tue Mar 05, 2019 6:40 am     Reply with quote

For numbers, I usually just go right ahead and include the stdint.h header. Then I can use uint8_t, int8_t, etc. And I know immediately whether they are signed or unsigned and their max/min values.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Tue Mar 05, 2019 7:46 am     Reply with quote

Ttelmah wrote:

'long'= int16 = 16bits.
One hex digit = 4bits.

if i want to use 4 units of 74595, so i need to use int32. is it correct?

int32= long ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Tue Mar 05, 2019 8:01 am     Reply with quote

Yes, and get_int32.

You made no mention before of multiple 74595's.

More likely to be really useful, is to use gethex, four times, and work
with four separate bytes. Honestly a lot easier to control pins with
something like

11223344 Hex, rather than 287454020.

With the former you can see 'at once' which bits are on and off.


Last edited by Ttelmah on Tue Mar 05, 2019 8:14 am; edited 1 time in total
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 1, 2  Next
Page 1 of 2

 
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