PDA

View Full Version : 16F690 or whatever SEROUT to PC



xnihilo
- 30th May 2008, 20:49
Hi,

I'd like to send an array of 70 bytes to a PC using the serial port.

Hardware connection:

I plan to connect PIC pin RA5 to DB9 pin 2 (RX) of the pc via an inline 1K resistor.
(By the way, my pic pin RA5 has a 100K weak pull down because of the way I use the pin when not using it to transfer data).

I put the following line at the top of the code:



Include "modedefs.bas"

'Predef config at flashing time

@ device pic16F690, intrc_osc_noclkout, BOD_OFF, PWRT_OFF, wdt_off, mclr_off, protect_off


I also use:



DEFINE OSC 8 'set OSC value to 8MHz for instructions PAUSE, PAUSEus and SOUND (instead of default 4MHz)

DEFINE CHAR_PACING 1000


and:



CLEAR

INTCON = 0 'disable interrupts and clear int flags

OSCCON = %01110001 'set osc to internal, HS 8MHz
'SETTINGS for this register:
'bit 7: not used: 0
'bits 6-4: IRCF bits: 8MHz internal oscillator: 111 (after a reset: auto sets to 110 = 4 MHz)
'bit 3: OSTS bit: device is running from one of the two internal sys clocks (HFOSC or LFOSC): 0
'bit 2: HTS bit: high freq 8MHz-125KHz status bit: shows if HFINTOSC is stable or not: 0
'bit 1: LTS bit: low freq 31Khz status bit: shows if LFINTOSC is stable or not: 0
'bit 0: SCS bit: sys clock selection bit: internal oscillator is used for system clock: 1

'''''''CMCON0 = %00000111 'comparators OFF, val = 7

CM1CON0.7 = 0 'disable comparator1
CM2CON0.7 = 0 'disable comparator2

ANSEL = %00000000 'choose digital i/o, val = 0
ANSELH = %00000000 'PIC16F690 specific

ADCON0.0 = 0 'turn adcon off (is already off by default)


OPTION_REG = %01111111 'enable porta weak pullups (no weak pull-up available for porta.3), p12
'bit7: pull ups are enabled by individual port latch values

'-------------------------------------------------------------------------------
TRISA = %111111 'all porta pins are set to input (high impedance) (portA.3 is always input pin, datasheet p5), beware of floating pins
'-------------------------------------------------------------------------------

'RA5 is used in default as an input, LOW (100K ext WPD) because it waits for a +5V pulse to bring it HIGH

TRISB = %0000

TRISC = %00000000 'all portc pins are set to output mode, PWM starts if CCP1CON is enabled

'-------------------------------------------------------------------------------
WPUA = %011111 'enable internal weak pull ups resistors individually on portA pins to bring them HIGH internaly, except for porta.3 for which the pullup is auto set if pin is enabled as MCLRE (Ra3 can be set as mclr or as i/o port with config words)
'-------------------------------------------------------------------------------
WPUB = %0000


'enable for RA0-4,(RA3 has an external 10k WPU, no internal WPU is available except automaticaly if used as MCLR)
'RA5 is LOW as default, it has an external 100k WPD (input as default).

'MEMO: when pic powers up all ports are set to inputs

'-------------------------------------------------------------------------------
PORTA = %011111 'set pins logic
'-------------------------------------------------------------------------------
PORTB = %0000
PORTC = %00000000 'set pins logic
'All are set to low by default, pins with pullups are brought automaticaly HIGH anyway


and just before the ON INTERRUPT I have:



'set as output low
TRISA.5 = 0
PORTA.5 = 0
FOR i = 128 to 197
read i,hits_count 'get the number of times player was hit by this opponent
SEROUT PORTC.5,T9600,[hits_count]
NEXT i
'restore as input low
TRISA.5 = 1
PORTA.5 = 0




What will be output? RS232 level so I can connect it directrly to the PC or should I use MAX232 chip???

Thanks!

mackrackit
- 30th May 2008, 21:06
What will be output? RS232 level so I can connect it directrly to the PC or should I use MAX232 chip???
if the distance is not to far, maybe less than 10 feet a 232 chip is not need . Just make sure you are using inverted mode for the serial.

xnihilo
- 30th May 2008, 21:39
if the distance is not to far, maybe less than 10 feet a 232 chip is not need . Just make sure you are using inverted mode for the serial.

Hello,

What is the output of the pic? TTL or RS232?
It should be less than 10 feet.

What is exactly the inverted mode?
I should be using N9200 instead of T9200 then?

mister_e
- 30th May 2008, 22:11
N9600 would work IF AND ONLY IF the internal OSC is accurate enough.

High likely i would use DEBUG instead, much more, i would use HSEROUT and a max232, max233 or whatever else RS232 driver.

You can reduce the in serie resistor if you want.

mackrackit
- 30th May 2008, 22:45
What is the output of the pic? TTL or RS232?

RS232


What is exactly the inverted mode?

Just as the name implies. The signal is inverted. Up is down and down is up.

HSEROUT is always TRUE. So the MAX232 or equal is needed for this.

I agree with Steve, get an external OSC and save your headache for someplace else.

mister_e
- 30th May 2008, 23:15
AND as you use the bit banging solution, you should make sure your Serial pin idle to the right level before sending any data.

true idle high
Inverted idle low.

xnihilo
- 31st May 2008, 11:20
I am using a 16F690 which has an USART, cool, but all pins are used, not cool... I can use RA5 before the program uses it for communicating with another pic.

Using Hserout would be nice and adding an external osc too but I don't have the pins available for that.

Well, so I am confused.

The only pin I have is RA5, what should I do...
Use SEROUT, right?
And use a max232 (in this case what benefit to I have if the pic is already outputing RSR232 level signal?).
Whith what setting for SEROUT if not N9200?
And can I or can I not use an external OSC?

mackrackit
- 31st May 2008, 11:56
The only pin I have is RA5, what should I do...
Re-design your project.


Use SEROUT, right?

Or DEBUG or SEROUT2


And use a max232 (in this case what benefit to I have if the pic is already outputing RSR232 level signal?).
In your case there is not a benefit. If you were sending a longer distance or using HSEROUT then you would need it.


Whith what setting for SEROUT if not N9200?
SEROUT -- N9600
SEROUT2 ---16468
DEBUGE --- DEFINE DEBUG_MODE 1


And can I or can I not use an external OSC?
If you mean internal...Maybe.
Temperature change can cause the internal to vary. It may work on the bench but not in a finished application.

xnihilo
- 31st May 2008, 12:26
The project is still my lasergame equipment.

When the "body" module is powered up, it will send 70 bytes to the PC (scores).
It will not have the time to heat too much do you think I still can use internal OSC at 8MHz, without a MAX232, with serout, N9600, a 1k inline resistor between pic RA5 and PC pin 2? Redesigning the project is unfortunately almost impossible now.

mackrackit
- 31st May 2008, 12:42
All I can say is to give it a try. Maybe in your case it will not be to much of a problem if communications do not work every time. Just try sending the score again??

The MAX232 will not help you.

If you find it works part of the time, you may try 2400 baud. Sometimes a slower signal is more reliable and more tolerant.

I am not sure, the internal on your chip might be able to be calibrated. Some one else will have to help you if it can or needs to be.

xnihilo
- 31st May 2008, 13:25
thank you for your answers

amgen
- 31st May 2008, 15:22
My experience with newer PIC's w/8mhz int osc is they can be counted on for good enough freq for serial stuff....!!??

don
amgen

Charles Linquis
- 1st June 2008, 02:30
The 8Mhz internal oscillator in most 18F PICs will work fine with SEROUT/SEROUT2 at up to 9600 baud. If you use DEBUGOUT, you can extend that to at least 19.2Kbaud.

Amazingly enough, I have used the 8MHz internal oscillator on an 18F2221 to communicate with another PIC at 38.4Kbaud (using DEBUGOUT). This worked across 16 parts, and all were tested to be fully operational while running in a temperature chamber -20C to +85C.

And as far as whether to use a MAX232 or not - the level coming out of a PIC does not meet RS-232 specs. While it usually works, I would never sell a product that way.

xnihilo
- 1st June 2008, 10:11
The 8Mhz internal oscillator in most 18F PICs will work fine with SEROUT/SEROUT2 at up to 9600 baud. If you use DEBUGOUT, you can extend that to at least 19.2Kbaud.

Amazingly enough, I have used the 8MHz internal oscillator on an 18F2221 to communicate with another PIC at 38.4Kbaud (using DEBUGOUT). This worked across 16 parts, and all were tested to be fully operational while running in a temperature chamber -20C to +85C.

And as far as whether to use a MAX232 or not - the level coming out of a PIC does not meet RS-232 specs. While it usually works, I would never sell a product that way.

Fine, RS232 levels out of the pic is not right. Can I use a MAX232 chip to boost the signal then? Is there a post with the recommended connexion? I have the datasheet with a "typical application" circuit...

Charles Linquis
- 1st June 2008, 14:35
Just use the connections shown in the MAX datasheet. If you are using SEROUT/SEROUT2 make certain you use the "TRUE" settings (idle high), because the MAX chip inverts the signal. Tx of the PIC to T1in of the MAX. R1out of the MAX to Rx of the PIC.