Serial Input

9 views
Skip to first unread message

Multitrain

unread,
Aug 14, 2025, 4:15:41 AMAug 14
to jallist
Hello, all

I have a problem with a serial input of an o lne activeld 16F628A.
I made a mistake but I am unable to find  it.
The program works when tested with the  line 88 active (pin_b4 used)
I tested the serial input data with a serial analyser and I receive the expected value for testing.
Here the program

; RX B Fab
; Recepteur pour télélcommande; Emetteur 433MHz à partir STX882 et PIC16F628 ou PIC 12F1572mais ttes les E/S
; seront prises.
; besoin: 4 entrées et 1 sortie, mais qud on utiise la fonction UART, une entrée
; est dédiée à la réception
; Gros pb, l'émetteur est tjrs ON.
; Avec le 16F628, une pin peut commander l'alim du TX et on a bcp de pin dispo
 ; comme sur aristo deux switch pour les direction
 ; dir_AV et dir_AR

include 16f628a                     -- target PICmicro
pragma target OSC INTOSC_NOCLKOUT
-- This program assumes the internal oscillator
-- is running at a frequency of 4 MHz.
pragma target clock 4_000_000      -- oscillator frequency
-- configuration memory settings (fuses)
pragma target WDT disabled   --  watchdog
pragma target MCLR internal  -- internal
pragma target CP disABLED
pragma target brownout ENABLED
pragma target LVP  disabled        -- no Low Voltage Programming

enable_digital_io()
include delay_pat

OPTION_REG_NRBPU = 0 ; pull up resistor sur portB 0n input

pin_a2_direction = output
alias Dir is pin_a2
pin_a3_direction = output
alias Xdir is pin_a3
pin_b3_direction = output
alias vit_pwm is pin_b3
include pwm_hardware

 const serial_hw_baudrate = 1200 ;1 bit:833 µs, 10 bits: 8,33 ms
 include serial_hardware
 serial_hw_init
; include serial_hardware_async_int_rx
; serial_hw_init_RX_Int()
 
var byte Vit = 3
var byte vit_step = 1
var byte vit_ref
var byte old_mot_x
var byte mot_x
var byte Data
var bit dir_old

var bit Acc
var bit Dec
var bit STP


var bit Dir_AR = on
var bit Dir_AV = off

var word freq = 21000

  pwm_set_frequency(Freq)
  pwm1_set_dutycycle_percent(3) ;3
  pwm1_on

--------------------------------------------------------------------------------
   procedure zoe is
     pragma interrupt
      mot_x = data

     if (mot_x  == 128) then Acc = on  else Acc = off  end if
     if (mot_x == 064)  then Dec = on  else Dec = off  end if
    ;if (mot_x == 32) then Dir = Dir_AV  end if
    ;if (mot_x == 16) then Dir = Dir_AR  end if
     if (mot_x == 240) then STP = on  else STP = off  end if
     PIE1_RCIE = on ; Enables the USART receive interrupt
     PIR1_RCIF = 0
   end procedure

--------------------------------------------------------------------------------

forever loop

if Dir_AV then Dir =  Dir_AV  Xdir = !Dir  end if
if Dir_AR then Dir = !Dir_AR  Xdir = !Dir  end if

 Data = serial_hw_data
  mot_x = data
;  if mot_x == old_mot_x then  ;  il faut que 2 mots consecutifs soient identiques
;if pin_b4 then mot_x = 128 else mot_x = 64 end if  ; pour test                              ; pour valier les données entrantes

  old_mot_x = mot_x

 if Acc then vit = vit + vit_step  pwm1_set_dutycycle_percent(Vit) end if
 if Dec then vit = vit - vit_step  pwm1_set_dutycycle_percent(Vit) end if
 if STP then vit = 3 end if
 if vit <= 5 then vit = 5 end if
 if vit >= 95 then vit = 95 end if
 vit_ref = vit
 
; if (dir!= dir_old) then
;         while vit >=3  loop pwm1_set_dutycycle_percent(Vit)
;               vit = vit - vit_step end loop
;          dir = !dir
;        while vit <= vit_ref loop pwm1_set_dutycycle_percent(Vit)
;               vit = vit + vit_step end loop
; end if
;
;dir_old = dir

  if STP then vit = 3 Acc = off  Dec = off
              pwm1_set_dutycycle_percent(Vit)
  end if

 _usec_delay(10_000)
end loop


Every good idea is welcomme
Patrick

pinhe...@gmail.com

unread,
Aug 14, 2025, 4:28:40 AMAug 14
to jallist
What is pin b4 connected to ?

--
You received this message because you are subscribed to the Google Groups "jallist" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallist+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/jallist/f403f3c5-859f-45b8-b62f-cf5f91747350n%40googlegroups.com.

Patrick FROUCHT

unread,
Aug 14, 2025, 7:04:18 AMAug 14
to jal...@googlegroups.com
I put it at ground or left ip (pull-up are activated) and I get aPWM pulse with an increasing or decreasing  duty cycle at the B3 output.

Patrick

pinhe...@gmail.com

unread,
Aug 14, 2025, 7:11:17 AMAug 14
to jal...@googlegroups.com
In a noisy environment, internal pull ups or pull down are sometimes too weak. ( input impedance too high)
To be on the safe side, i always,add an external resistor (unless the pin is driven by a full digital signal)

Patrick FROUCHT

unread,
Aug 14, 2025, 9:06:34 AMAug 14
to jal...@googlegroups.com
I used pin _B4 only for test purposes and it worked very fine with the pull-up. so the program is OK.
 I just want to know why my serial input does not work,

Patrick

Rob CJ

unread,
Aug 14, 2025, 12:36:31 PMAug 14
to jal...@googlegroups.com
Hi Patrick,

Pin_b4 is not defined in your program and when I remove these two commented lines then I get also a compiler error since and end-if is missing.

;  if mot_x == old_mot_x then  ;  il faut que 2 mots consecutifs soient identiques
; if pin_b4 then mot_x = 128 else mot_x = 64 end if  ; pour test                              ; pour valier les données entrantes


What is exactly the program and what is not working?

Kind regards,

Rob



Van: jal...@googlegroups.com <jal...@googlegroups.com> namens Patrick FROUCHT <patfr...@gmail.com>
Verzonden: donderdag 14 augustus 2025 15:06
Aan: jal...@googlegroups.com <jal...@googlegroups.com>
Onderwerp: Re: [jallist] Serial Input
 

Patrick FROUCHT

unread,
Aug 14, 2025, 2:05:19 PMAug 14
to jal...@googlegroups.com
semicolHello,
I made my program easier to read
 Data = serial_hw_data
  mot_x = data

;if pin_b4 then mot_x = 128 else mot_x = 64 end if  ; for testing only        ; pour valider les données entrantes


  old_mot_x = mot_x

 if Acc then vit = vit + vit_step  pwm1_set_dutycycle_percent(Vit) end if
 if Dec then vit = vit - vit_step  pwm1_set_dutycycle_percent(Vit) end if
 if STP then vit = 3 end if
 if vit <= 5 then vit = 5 end if
 if vit >= 95 then vit = 95 end if
 vit_ref = vit


 _usec_delay(10_000)
end loop

The main loop of the program makes if you receive 128 on serial input, the 20 kHz PWM output increases its duty cycle up to 95%. If you receive a value of 64  the 20 kHz PWM output decreases its duty cycle up down to 5%.
To test this feature I remove the semicolonand and I simply put a wire  on Pin_b4. When the wire is not connected I get the value 128 for mot_x and 64  when connected to gnd.
That means the program uses the "zoe" procedure and changes the PWM duty cycle regarding the value  (128 or 64) associated with the state of  pin_b4.

What is not working is the serial input
With an external system (another PIC) I send continuously  via the serial output the value 64 or 128 .
I checked with a serial tester and it shows on the input the values of 64 or 128 depending on what I wanted for the PWM output.
 So the serial data for testing is OK.
I have also put the "  PIE1_RCIE = on ; Enables the USART receive interrupt" in the main program just at the beginning of the program ...nothing.

What is wrong?
Any help Welcome
THX

Patrick





Rob CJ

unread,
Aug 14, 2025, 2:50:19 PMAug 14
to jal...@googlegroups.com
Hi Patrick,

How fast are you sending the values of 64 and 128?

You are using the standard serial hardware so without any buffering and you have a 10 ms looptime so if you send the serial data too fast you will lose characters.

Next to that I do not understand why you are using an interrupt routine. The serial hardware library disables the serial interrupt since it does not use it. And if you do not handle an interrupt well, the PIC will get stuck in the interrupt and will never leave it. 

Have you tried it with the serial_hw_int_cts.jal library? That works with interrupts and you do not have to take care of that.

What was your intention with the interrupt routine?

Kind regards,

Rob


Verzonden: donderdag 14 augustus 2025 20:05

Rob CJ

unread,
Aug 14, 2025, 4:00:19 PMAug 14
to jal...@googlegroups.com
Hi Patrick,

I guess - not sure - that you want to run the zoe routine when you receive serial data only.

If that is the case I changed your program using the serial_hw_int_cts library, see attachment.

For compiling I needed to temporary comment out the 'delay_pat' since I do not have that but I restored it.

Is this what wanted to achieve?

Kind regards,

Rob


Van: jal...@googlegroups.com <jal...@googlegroups.com> namens Rob CJ <rob...@hotmail.com>
Verzonden: donderdag 14 augustus 2025 20:50
Serial_RJ.jal

Patrick FROUCHT

unread,
Aug 14, 2025, 4:15:50 PMAug 14
to jal...@googlegroups.com
I thought interrupt was mandatory with the serial input...A bad thought!
So sorry  I'll give it a try tomorrow morning without the interrupt routine and tell you what will happen. 

I use the interrupt in the transmitter, because it works like a TV remote. When no contact is done, the system enters the sleep mode to reduce the power during no use. Once a contact is made (I have 5 contacts so I use the pin_B4 to BT with interrupt on change  and pin_B0 as interrupt for the fifth contact to wake up the transmitter from sleep.
So I stuck with interrupt.  

THX again
Patrick


Patrick FROUCHT

unread,
Aug 14, 2025, 4:37:49 PMAug 14
to jal...@googlegroups.com
I have made the library "delay_pat" and also I use the random_3 for random function because they use byte and no word. Operation with words  are memory consuming and it is not welcomed with pic like 10F222 ,322 even 12f629 with very small memories.

Patrick

Patrick FROUCHT

unread,
Aug 15, 2025, 4:29:17 AMAug 15
to jal...@googlegroups.com
Thanks Rob

it works well after minor timing adjustment on the TX  and   RX side.
Now I can continue the RX side development with the direction requirements. .
Again THX

Patrick
Reply all
Reply to author
Forward
0 new messages