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

rs232 LED BLINK pıc-to-pıc communication
Goto page Previous  1, 2, 3, 4, 5, 6  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Khansokhua



Joined: 06 Nov 2021
Posts: 88

View user's profile Send private message

PostPosted: Fri Mar 24, 2023 6:08 am     Reply with quote

Here I tried to modify sisr.c file.It seems I couldn't even move the motor.
Code:
 #include <16F877A.h>
#FUSES HS, NOWDT,NOPROTECT, NOPUT , NOLVP,  BROWNOUT               
#use delay(clock=20000000)
#use rs232(baud=4800,BITS=8, PARITY=N,XMIT=pin_C6, RCV=pin_C7, STREAM=SPIC,ERRORS)
#use standard_io(D)

#define use_servo_1
#define  servo_1 pin_d0
#include <servo_st.c>
#include <map_function.c>
 
 
 
#define BUFFER_SIZE 8
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;


#int_rda
void isr(){
 

   int t;

   buffer[next_in]=getc();
   t=next_in;
   next_in=(next_in+1) % BUFFER_SIZE;
   if(next_in==next_out)
     next_in=t;           // Buffer full !!
     
}
#define bkbhit (next_in!=next_out)

BYTE bgetc() {
   BYTE c;

   while(!bkbhit) ;
   c=buffer[next_out];
   next_out=(next_out+1) % BUFFER_SIZE;
   return(c);
}
void main()
{
      enable_interrupts(int_rda);
      enable_interrupts(global);
     
 
    while(1){


       if(bkbhit)
       {
       delay_ms(20);
        servo_1_write(bgetc());
       }   

            } 

}
PrinceNai



Joined: 31 Oct 2016
Posts: 452
Location: Montenegro

View user's profile Send private message

PostPosted: Fri Mar 24, 2023 6:18 am     Reply with quote

What I meant is if you connect the servo with a PIC, can you control it the way you want with a pot? Without any serial? Does the motor behave correctly? In other words, do your motor control routines work?

When you say "I guess" for the reception. What do you mean with that? What tool are you using for debugging? Have your master PIC sending out a known character every second. Send it forward on a slave on a terminal or on a display to verify that what you sent is what you received. You could even hook 8 LED's on a port and send the character you received there.

It is very hard to help you if you don't tell us which parts of your application are actually working.
Khansokhua



Joined: 06 Nov 2021
Posts: 88

View user's profile Send private message

PostPosted: Fri Mar 24, 2023 7:00 am     Reply with quote

PrinceNai wrote:
What I meant is if you connect the servo with a PIC, can you control it the way you want with a pot? Without any serial? Does the motor behave correctly? In other words, do your motor control routines work?

Yes, it works well on master pic. I can control using potentiometer 0-180 degree.

Also, I don't use any debugger. I am confused right now about code algorithm in sisr.c.
PrinceNai



Joined: 31 Oct 2016
Posts: 452
Location: Montenegro

View user's profile Send private message

PostPosted: Fri Mar 24, 2023 7:15 am     Reply with quote

I presume your servo responds to signals between 1000us and 2000us. Does this move it?
Code:

unsigned int16 PulseWidth;
#define servo_1 PIN_D0
int8 i;

void main()
{
   while(TRUE)
   {
// full left
      PulseWidth = 1000;
      for(i=0;i<=100;i++){
         output_high(servo_1);                                 // send a pulse to servo
         delay_us(PulseWidth);
         output_low(servo_1);
         delay_us(20000 - PulseWidth);                         // wait for the remainder of 20ms
         delay_cycles(1);
      }
// center     
      PulseWidth = 1500;
      for(i=0;i<=100;i++){
         output_high(servo_1);                                 // send a pulse to servo
         delay_us(PulseWidth);
         output_low(servo_1);
         delay_us(20000 - PulseWidth);                         // wait for the remainder of 20ms
         delay_cycles(1);
      }
// full right     
      PulseWidth = 2000;
      for(i=0;i<=100;i++){
         output_high(servo_1);                                 // send a pulse to servo
         delay_us(PulseWidth);
         output_low(servo_1);
         delay_us(20000 - PulseWidth);                         // wait for the remainder of 20ms
         delay_cycles(1);
      }     
     
   }        // while true

}           // main
PrinceNai



Joined: 31 Oct 2016
Posts: 452
Location: Montenegro

View user's profile Send private message

PostPosted: Fri Mar 24, 2023 7:22 am     Reply with quote

Ok, so it works on a master. How do you do it? Read ADC and feed the value into your servo control routine? How fast are you reading ADC and changing the width of pulses? Would you share the code that works on a master?

What are you using to program the PIC's?
Khansokhua



Joined: 06 Nov 2021
Posts: 88

View user's profile Send private message

PostPosted: Fri Mar 24, 2023 7:36 am     Reply with quote

PrinceNai wrote:
Ok, so it works on a master. How do you do it? Read ADC and feed the value into your servo control routine? How fast are you reading ADC and changing the width of pulses? Would you share the code that works on a master?

What are you using to program the PIC's?

pickit3


Code:
#include <16F877A.h>
#device adc=8
#FUSES HS, NOWDT,NOPROTECT, NOPUT , NOLVP,  BROWNOUT               
#use delay(clock=20000000)

#use standard_io(D)

#define use_servo_1
#define  servo_1 pin_d0
#include <servo_st.c>
#include <map_function.c>
void main()
{
       setup_adc_ports(AN0); // POT WIRED
       setup_adc(ADC_CLOCK_DIV_32);
        set_adc_channel(0);
       delay_us(20);
       servo_init();
   while(TRUE)
   {
     
     unsigned int8 valor_adc=read_adc();
    unsigned int8 valor_pwm=map(valor_adc,0,255,0,195);
     servo_1_write(valor_pwm);
   }

}
PrinceNai



Joined: 31 Oct 2016
Posts: 452
Location: Montenegro

View user's profile Send private message

PostPosted: Fri Mar 24, 2023 8:04 am     Reply with quote

So you do have a debugger, just not using it :-)
Code:

   while(TRUE)
   {
     
     unsigned int8 valor_adc=read_adc();
    unsigned int8 valor_pwm=map(valor_adc,0,255,0,195);
     servo_1_write(valor_pwm);
   }

Two comments. You are doing it too fast. Servo expects pulses every 20ms, but maybe your servo_1_write function is taking care of that. The second is regarding ADC readings. There is no filtering, read averaging. I'd suspect the motor is not quite stable because of that. Search for the thread "olympic averaging". Read ADC multiple times and apply it on those readings.

Now for the receiving part. Master only reads ADC, nothing else. Does the averaging. It is not necessary to do it every 20ms. That depends on the refresh rate you want. How often do you want to change the position of the servo? Maybe once per second is enough? Anyway, when you get the ADC value, simply send it out over serial. For testing purposes, send out a known value. Put the slave PIC into debugging mode (pick Pickit3 as the debugger in MPLAB, import .cof file and open .c file). Then check what came over the serial.
PrinceNai



Joined: 31 Oct 2016
Posts: 452
Location: Montenegro

View user's profile Send private message

PostPosted: Fri Mar 24, 2023 8:09 am     Reply with quote

For debugging you'll have to pick CCS as the active toolsuite. And download MPLAB plugin from CCS.
temtronic



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

View user's profile Send private message

PostPosted: Fri Mar 24, 2023 12:51 pm     Reply with quote

also
if you use 'debug', you have to change build configuration to 'debug',then compile
AND
you'll need to select 'release' in the build configuration, then compile,again to make the proper code for the real hardware.
Khansokhua



Joined: 06 Nov 2021
Posts: 88

View user's profile Send private message

PostPosted: Tue Mar 28, 2023 1:28 pm     Reply with quote

Greetings...Here we are, 2xPIC16F877A
I use HCSR-04 ultrasonic distance sensor and measure between 1 cm and 50 cm.
I see and measure correct for real. When I wired tx-rx rx-tx, I still measure correct values, no problem.
But when I wired 433 MHz cheap rf module,

rx
Code:

while(TRUE)
{
        lcd_gotoxy(1,1);
        lcd_putc("Sensor HC-SR04");
        lcd_gotoxy(1,2);
        delay_ms(250);
        distance=getc();
       
        if(distance<=50 && distance >=1){
        printf(lcd_putc,"Dist: %d cm  ",distance ); // I can't see any value for
                                                         second line, just monitoring first line for a while                                   
                                          //I make observation but not any change observed.
                                                                                     
                                          // It stucks a value and stays.
        }
        if(distance==51){lcd_putc("OUT of RANGE");}
}

tx
Code:
 while(TRUE)
   {
      distance=HCSR04_get_distance();
      putc(distance);             
     
      delay_ms(150);
   }


I want to see some value even it is false, what should I do? What is that I am overlooking again?
temtronic



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

View user's profile Send private message

PostPosted: Tue Mar 28, 2023 3:19 pm     Reply with quote

re: 433MHz cheap rf module

You really need to post a link to this module.
Some are well known to cause problems, others are good for over 1Km !

You may have a power supply issue, a 'wakeup the module ' issue, 5V vs 3V issue (real common), setup-module issue.

Can't tell that until we see the datasheet for your modules.
Khansokhua



Joined: 06 Nov 2021
Posts: 88

View user's profile Send private message

PostPosted: Tue Mar 28, 2023 4:36 pm     Reply with quote

temtronic wrote:
You really need to post a link to this module.
Some are well known to cause problems, others are good for over 1Km !

You may have a power supply issue, a 'wakeup the module ' issue, 5V vs 3V issue (real common), setup-module issue.

Can't tell that until we see the datasheet for your modules.


https://www.robotpark.com/433Mhz-RF-Transmitter-and-Receiver
temtronic



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

View user's profile Send private message

PostPosted: Tue Mar 28, 2023 4:58 pm     Reply with quote

yes, had a feeling it that was that unit....

they are NOT 'RF modems' that are RS-232 type compatible devices so you cannot simply use the 'UART' form of communications(#USE RS-232(...options...).

Interesting that the vendor doesn't supply 'documentation' on how they work or how to operate them. You might search this forum, pretty sure someone years ago has used them. Maybe use Google to locate the steps and code you need .
I recall that you needed to send 'something' to wakep the rcvr unit, then xmt your data, but that was years ago... I went with real RF Modems. They have a microcontroller in them, accept true serial (aka RS-232) data .

here is one example of what you need to find..
https://simple-circuit.com/rf-remote-control-pic-microcontroller/


using google "433 transmitter ccs c code"

Note. this is NOT my code, just the 1st one I saw of several 1,000s of 'hits'. It does NOT send 'RS-232 data' but may help you.

here's another, might be closer to what you need....
https://www.instructables.com/Wireless-Communication-Using-Cheap-433MHz-RF-Modul-1/

the 'preamble' is needed to adjust the AGC of the rcvr module
Khansokhua



Joined: 06 Nov 2021
Posts: 88

View user's profile Send private message

PostPosted: Tue Mar 28, 2023 8:12 pm     Reply with quote

temtronic wrote:
yes, had a feeling it that was that unit....

they are NOT 'RF modems' that are RS-232 type compatible devices so you cannot simply use the 'UART' form of communications(#USE RS-232(...options...).

Interesting that the vendor doesn't supply 'documentation' on how they work or how to operate them. You might search this forum, pretty sure someone years ago has used them. Maybe use Google to locate the steps and code you need .
I recall that you needed to send 'something' to wakep the rcvr unit, then xmt your data, but that was years ago... I went with real RF Modems. They have a microcontroller in them, accept true serial (aka RS-232) data .

here is one example of what you need to find..
https://simple-circuit.com/rf-remote-control-pic-microcontroller/


using google "433 transmitter ccs c code"

Note. this is NOT my code, just the 1st one I saw of several 1,000s of 'hits'. It does NOT send 'RS-232 data' but may help you.

here's another, might be closer to what you need....
https://www.instructables.com/Wireless-Communication-Using-Cheap-433MHz-RF-Modul-1/

the 'preamble' is needed to adjust the AGC of the rcvr module


To wire 220nF cap between GND pin and Vcc pin will increase the performance of the module?
temtronic



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

View user's profile Send private message

PostPosted: Wed Mar 29, 2023 4:55 am     Reply with quote

no, your software is wrong. you need to rewrite it to include the 'preamble' the 2nd link wrote about
Or locate similar articles about using those tx-rx modules.
There is a LOT of them in Ardunio forums and their code can easily be translated into CCS C.
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, 4, 5, 6  Next
Page 4 of 6

 
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