PDA

View Full Version : HELP! HSER issues from old to new



wdmagic
- 17th October 2015, 02:56
I had a couple of projects sending serial data to LCD's and that still works as far as i can tell, I don't have the displays to check. i also have a VB.net program that sends a single byte to a chip and that chip displays it on a parallel LCD, note that should still work too. my problem is I am using a 8pin chip with no LCD, all I have is a LED to see if i'm getting data on the chip. I am also using the software "realterm" as a temporary interface till the VB program is written. i have tried for 4 hours now and its still not working, many configurations and such so I will state what I need and hopefully someone can insert missing code or provide links etc... Project: Use a 12F1572 to read a 16 bit analog pin, send that 16bit data to the PC, then wait for response. PC will take number do some math, etc.. and then send a byte back to the chip to tell the chip to turn on a output pin to drive a LED or FET. heres my basic layout of code define OSC 4 DEFINE ADC_BITS 16 ' A/D number of bits DEFINE ADC_CLOCK 3 ' Uses A/D internal RC clock DEFINE ADC_SAMPLEUS 50 ' Set sampling time in us define HSER_BAUD 9600 DEFINE HSER_BITS 8 DEFINE HSER_CLROERR 1 DEFINE HSER_RCSTA 90h DEFINE HSER_TXSTA 20h DEFINE HSER_SPBRG 25 DEFINE HSER_SPBRGH 0 A VAR WORD 'Stores 16 Bit Analog Reading B VAR byte 'Temp Holder Switch VAR PORTA.5 'Controls External FET or LED on pin 2 Switch = 0 mainloop: ADCIN 2, A 'Grab analog input from Pin 3 Hserout [A] ' Send char out serial port pause 100 HSERIN [B] if B = 5 then switch = 1 else switch = 0 pause 100 goto mainloop ' Do it all over again

wdmagic
- 23rd October 2015, 00:58
Ok I found a way around to get my code to show up right, I could really use some help getting this to work.
I have a project that needs this interface and I want to get it running by the 28th before I get back home.



define OSC 4

DEFINE ADC_BITS 16 ' A/D number of bits
DEFINE ADC_CLOCK 3 ' Uses A/D internal RC clock
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in us

define HSER_BAUD 9600
DEFINE HSER_BITS 8
DEFINE HSER_CLROERR 1
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 20h
DEFINE HSER_SPBRG 25
DEFINE HSER_SPBRGH 0

A VAR WORD 'Stores 16 Bit Analog Reading
B VAR byte 'Temp Holder
Switch VAR PORTA.5 'Controls External FET or LED on pin 2
Switch = 0

mainloop:
ADCIN 2, A 'Grab analog input from Pin 3

Hserout [A] ' Send char out serial port
pause 100

HSERIN [B]
if B = 5 then switch = 1 else switch = 0
pause 100

goto mainloop ' Do it all over again

Archangel
- 23rd October 2015, 03:40
Hi WD,
I am not sure which part you are having trouble with, and am presently unable to work on HSER*** but I played with your code using a 16F690 demo board and debug and pretty well got it to function, accuracy notwithstanding . . .


@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF
; DEFINE LOADER_USER 1 ' Boot-Loader is being used
DEFINE OSC 4 ' Change to your chip
DEFINE DEBUG_MODE 0 ' Debug sending True serial data
DEFINE DEBUG_REG_PORTA ' Debug Port = PortA as required by PICKIT2 serial Monitor
DEFINE DEBUG_BIT 0 ' Debug.bit = PortA.0
DEFINE DEBUG_BAUD 9600 ' Default baud rate = 9600
DEFINE DEBUGIN_REG PORTA ' Debug Port = PortA as required by PICKIT2 serial Monitor
DEFINE DEBUGIN_BIT 1 ' Debugin bit PortA.1
DEFINE DEBUGIN_BAUD 9600 ' Default baud rate = 9600
DEFINE DEBUGIN_MODE 0 ' Debugin receiving data true = 0 inverted = 1

;DEFINE HSER_BAUD 9600 ' override the default of 2400
ANSEL = 0 '
ANSELH = 0
ADCON0 = %00001011
led VAR portc.1 ' change if needed

TrisC = %00000000
TrisA = %00000010
A VAR word 'Stores 16 Bit Analog Reading
B0 VAR byte 'Temp Holder
a=0
b0=0
Switch VAR byte
;portC = %00000000
mainloop:
ADCIN 2, A 'Grab analog input from RA2

;
pause 100

Debugin [B0]
IF B0 = "5" THEN
led = 1
Debug " high fives"

else
led = 0
Debug " still low"



pause 100
Debug B0 ' Send char out serial port
endif
A = a + 48
debug A

goto mainloop ' Do it all over again



Maybe something useful in there.
JS

wdmagic
- 25th October 2015, 19:14
that didnt help unfortunatly. I did get some stuff to work, as it seems I forgot my TRIS statement which always causes problems
however here is my updated code


Include "12F1572.bas"
DEFINE OSC 20

DEFINE ADC_BITS 16 ' A/D number of bits
DEFINE ADC_CLOCK 3 ' Uses A/D internal RC clock
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in us

define HSER_BAUD 9600
DEFINE HSER_BITS 8
DEFINE HSER_CLROERR 1

ADCON0.7 = 0
TRISA = 0

A VAR WORD 'Stores 16 Bit Analog Reading
B VAR byte 'Temp Holder

mainloop:
ADCIN 3, A 'Grab analog input from pin3
porta.2 = 0
Hserout [a,13,10] ' Send char out serial port
pause 250
porta.2 = 1
pause 250
goto mainloop ' Do it all over again


now if i chage


Hserout [a,13,10]

to


Hserout ["help",13,10]

then i get help on the computer.
still at a loss here, I'm missing something

Ioannis
- 25th October 2015, 20:48
I think you are sending out the binary value of a and not the converted to ascii value.

your HSEROUT should be something like this:



HSEROUT [dec5 a,13,10]


or



HSEROUT [#a,13,10]


Ioannis

wdmagic
- 26th October 2015, 22:16
ok that worked a lot better. However I don't thing my justify bits is working as my results are all from 512 to 64k not from 0 - 1024

here is my adjusted code



Include "12F1572.bas"
DEFINE OSC 20

DEFINE ADC_BITS 10 ' A/D number of bits
DEFINE ADC_CLOCK 1 ' Uses A/D internal RC clock
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in us

define HSER_BAUD 9600
DEFINE HSER_BITS 8
DEFINE HSER_CLROERR 1

ADCON0.7 = 0
TRISA = %00000100
ANSELA = %00000100

A VAR word 'Stores 10 Bit Analog Reading
mainloop:
ADCIN 2, A 'Grab analog input from pin3
pause 50
Hserout [#A,13,10] ' Send char out serial port
pause 500
goto mainloop ' Do it all over again

Ioannis
- 27th October 2015, 11:43
What if you comment out the line ADCON0.7 ?

Ioannis

tumbleweed
- 27th October 2015, 12:46
However I don't thing my justify bits is working as my results are all from 512 to 64k not from 0 - 1024
In that pic, the ADFM 'justify' bit is in ADCON1.7

wdmagic
- 27th October 2015, 14:41
ok remarking out - didnt work
ADCON1.7 = 0 - didnt work
ADCON1.7 = 1 - WORKED!

OK so now we are sending to the computer what we need. Now to get the part to receive from the PC a Byte.

Heres my modified code, its been reworked several times stuff moved round Ill make notes at bottom of what its doing.



Include "12F1572.bas"
DEFINE OSC 20

DEFINE ADC_BITS 10 ' A/D number of bits
DEFINE ADC_CLOCK 1 ' Uses A/D internal RC clock
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in us

define HSER_BAUD 9600
DEFINE HSER_BITS 8
DEFINE HSER_CLROERR 1

ADCON1.7 = 1
TRISA = %00000100
ANSELA = %00000100

A VAR word 'Stores 10 Bit Analog Reading
B var byte

mainloop:
b = 0
porta.4 = 1
pause 250
porta.4 = 0
HSERIN 250, test,[B] : Pause 50
goto mainloop ' Do it all over again

test:
ADCIN 2, A 'Grab analog input from pin3
pause 250
Hserout [#A,13,10] ' Send char out serial port
porta.4 = 1
pause 1000
porta.4 = 0
goto mainloop


ok, it goes in blinks the led quick and starts the line of code for hserin then it jumps to the test loop runs the adc and sends to the pc and the led continues to blink at 1 sec intervals, it never does the quick led blink of the main loop when test loop led duration is longer. and im not sure if I have the HSERIN coded right, it was taken from other code I had working last year with another chip. the weird thing is, if I change the mainloop led to 1000 and the test loop to 250, it still blinks at the 1 sec rate????

Ioannis
- 28th October 2015, 17:31
If it does not do the fast blink, that may imply that there is data on the Serial port. Even one character is enough to exit from the HSERIN loop.

Maybe it is better to have a different LED port for the mainloop and another for the test loop.

Ioannis

wdmagic
- 1st November 2015, 17:08
yes that would be good, but I have no pins left :(

1 +
2 Serial
3 Serial
4 Analog IN
5 Clock
6 LED
7 Digital input only/MCLR (unused)
8 -

Can I use a led going from b+ to LED to 7 and use the chip to sink the LED current?

Ioannis
- 1st November 2015, 22:11
No, you cannot because this pin for the 12F1572 is only input.

Ioannis