Send a byte on a pin

Thread Starter

pjreijiri

Joined Aug 19, 2015
116
Just one note, correct me if I'm wrong, but the frequency is 8Mhz which gives me a shorter period. even after Fosc/4 it would give me 0.5us
 

Sensacell

Joined Jun 19, 2012
3,785
The code to receive is more complex than to send.

You can see that if your clock period is 500ns, trying to bit-bang DMX data rate leaves only time for 8 CPU instructions per bit.
Probably not workable.
 

Sensacell

Joined Jun 19, 2012
3,785
8 instructions per bit is not enough granularity to effect clean serial transmission.

it works if the baud rate is much much slower than the instruction clock, you can then balance the timings and make it work.
Using a timer interrupt for example, this would make the timing consistent, even while the CPU was running other tasks.
However, 8 instructions is already more than it would take to handle the basic interrupt housekeeping tasks.

What is the baud rate of your Bluetooth module?
 

jpanhalt

Joined Jan 18, 2008
11,087
The 16F887 is spec'd up to 20 MHz (200 ns per Tcy). A bit-banded version of transmit and do it in roughly 10 Tcy = 2 us per bit. Here is some code that was originally from Mike McLaren (K8LH). He may have shortened it since. It is for a different chip,but translating it to the 16F887 shouldn't be difficult.

Code:
;******************************************************************************
;                             PUT232
; Put232 (9600 baud) -- Mike McLaren, K8LH (Jan-09)                                                   
;******************************************************************************

Put232
     movlb     2
     movwf     txbyte          ; save Tx data byte                       
     movlw     10              ; 1 start + 8 data + 1 stop bit             
     movwf     bit_ctr         ; setup bit counter                         
     bcf       status,0        ; C = 0 (start bit)                         
     DelayCy   (104*usecs-10)  ; 104 usecs minus 10 cycles(added)          
     goto      SendBit         ; send start bit                           
NextBit
     DelayCy   (104*usecs-10)  ; 104 usecs minus 10 cycles                 
     bsf       status,0        ; always shift in a 'stop' bit             
     rrf       txbyte,f        ; put data bit in Carry                     
SendBit
     movf      LATD,W          ; read port                                 
     iorlw     1<<1            ; set Ser_out pin bit to 1                 
     btfss     status,0        ; if data bit = 1 skip, else               
     xorlw     1<<1              ; set Ser_out pin bit to 0                
     movwf     LATD            ; precise update intervals                 
     decfsz    bit_ctr,f       ; done? yes, skip, else                     
     goto      NextBit         ; send next bit                             
     retlw     0               ;
John
 
Last edited:
Top