PDA

View Full Version : Newbie Question



Travin77
- 24th February 2006, 23:56
How do I make a RTC using tmr1 and 32 mhz crystal with out a dallas clock? I also need to be able to set time with buttons. Using pic 16f628a. Also can I send serial data on any port pin on only those labeled rx/tx? Not sure how to do all of that. I am new at this so please be gentle. Thanks for your help

paul borgmeier
- 25th February 2006, 08:33
Travin77,

See this post

http://www.picbasic.co.uk/forum/showthread.php?t=2129

It describes how to make a “RTC” without a RTC chip using a 4.0MHz xtal and TMR0. The approach can easily be modified for other xtal frequencies and TMR1. The first program in the referenced post has some button routines for setting the initial time.

The RX and TX Pins can be connected to the on-board hardware USART (via register settings). To use these two pins in this manner, you can use the HSERIN and HSEROUT commands. If you do not need the capabilities of the USART, you can use any pin that can be made an output (including the pins marked RX and TX) and the DEBUG, SEROUT, or SEROUT2 commands or any pin that can be made an input and the SERIN and SERIN2 commands. See the manual and datasheet for more information on this. Good Luck,

Paul Borgmeier
Salt Lake City, Utah
USA

DynamoBen
- 25th February 2006, 17:00
Some advice: I do a lot of serial communications with pics. I always use the USART. The reason being is I don't have to constantly poll the pins to receive data. When data comes in the program is interrupted and I handle the data as needed. Also the USART buffers data, so I never lose anything.

Here are the things you will need to get it to function:

DEFINE HSER_BAUD 9600 ' 9600 Baud USART
DEFINE HSER_RCSTA 90h ' Enable USART RX
DEFINE HSER_TXSTA 24h ' Enable USART TX
DEFINE HSER_CLROERR 1 ' Clear all USART errors as they happen

ON INTERRUPT GoTo INTERRUPT_SUB
PIE1.5=1 ' Enable USART RCIF interrupt flag


INTERRUPT_SUB :
IF PIR1.5=1 Then HSerin [Data] ' If RX flag is set retrieve serial data

This should give you a running start if you use it.

Travin77
- 26th February 2006, 01:18
Thanks for the code. I almost got it to work i think. The problem is I don't know how to adjust/configure my registers. I looked at both the 12f625 and 16f629a datasheets to compare the original code to the updated code but my pic has alot registries naturally. I am also not sure if I have the crystal pins setup correctly or not. Here is my code and schematic. The oscillator has built in caps. I also forgot to ask what code I would need to make something happen at a given time. Thanks for any help you all can give.

DynamoBen
- 26th February 2006, 05:51
Several things:

1) First your crystal is connected incorrectly based on the text file. On a 16F628 the crystal needs to be tied to pins 15 and 16. (see datasheet for details)

2) Depending on your baud rate you may want to move the speed of your crystal up. At 9600 baud I usually use 16 mhz crystal.

3) Here is an example of a typical Device programming Options :

@ DEVICE pic16F628, HS_OSC ' System clock Options
@ DEVICE pic16F628, WDT_ON ' Watch Dog Timer
@ DEVICE pic16F628, PWRT_ON ' Power-on timer
@ DEVICE pic16F628, BOD_ON ' Brown-out detector
@ DEVICE pic16F628, MCLR_OFF ' Master Clear Options
@ DEVICE pic16F628, LVP_OFF ' Low-voltage programming
@ DEVICE pic16F628, CPD_OFF ' Data Memory Code Protect
@ DEVICE pic16F628, PROTECT_OFF ' Program Code Protections

NOTE: If you noticed above I have clock option set to HS not XT. At 16mhz this is considered High-Speed.

4) When using a crystal don't forget to define the speed:
DEFINE OSC 16 ' 16mhz XTAL

5) The word "SYMBOL" is unneeded when you are defining constants. Use the follwing instead:
line1 CON 128

6) TRISB=%11111111 ' PORTB all Inputs
TRISB=%00000000 ' PORTB all Outputs
TRISB = %00000001 ' PORTB.0 PIN input for button

These things should help get you running.

paul borgmeier
- 26th February 2006, 07:36
With your code posting, if you fix the crystal location as DynamoBen suggested (and makd sure to add the caps to GND) and delete the “INTCONB=0 'interrupts off”
line, the clock portion with display should work great as is.

Note - for the code you have, you need to use 4MHz xtal. You could adjust the algorithm for 16MHz xtal if desired. One last thing, to “use the clock”, you can put something like

If HH = 4 then ‘ turn on portb.1 at 4:15 and turn it off at 4:30
If MM = 15 then portb.1=1
If MM = 30 then portb.1=0
Endif

In the section labeled “do something here”

Again, Good Luck,

Paul Borgmeier
Salt Lake City, Utah
USA

Travin77
- 26th February 2006, 20:42
I also have a 20mhz crystal. Would this be better to use? Also, I really don't know how to adjust the algorithim for any change in crystal speed, how would you do this? Additionally, on ProtectionsDynamoBen's post, do I need to include the following in the program:
@ DEVICE pic16F628, HS_OSC ' System clock Options
@ DEVICE pic16F628, WDT_ON ' Watch Dog Timer
@ DEVICE pic16F628, PWRT_ON ' Power-on timer
@ DEVICE pic16F628, BOD_ON ' Brown-out detector
@ DEVICE pic16F628, MCLR_OFF ' Master Clear Options
@ DEVICE pic16F628, LVP_OFF ' Low-voltage programming
@ DEVICE pic16F628, CPD_OFF ' Data Memory Code Protect
@ DEVICE pic16F628, PROTECT_OFF ' Program Code
I noticed after time setup, the program changes the contitions of some of the registries. Do I need to do the same and what should they be?

Sorry for being a hassle but I really appreciate you alls help on this.

Thanks

DynamoBen
- 26th February 2006, 21:15
The @ DEVICE statements are always at the top of my program. It automatically sets up my programmer when I load the file.

If you change the crystal speed you will need to alter your timing. For now I would just mess around with 4mhz until you find your way around. If it works then great!

The reason you need to adjust as you change the crystal speed is because the timer function is directly proportional to the crystal. Faster the crystal the faster the timer function will interrupt. If you move to a 20mhz crystal your interrupt will be 5 times faster. So instead of 1 second in your clock program it will be 200ms.

One thing to remember, most everyone here learned by reading the manual and experimenting. You may have to experiment to answer some of your questions. You may want to pick up Nuts and Volts magazine this month. They are doing a series on PICs and PICBasic, very good if you’re just starting out.
http://www.nutsvolts.com/

Travin77
- 26th February 2006, 22:53
Thanks for the help, I just dont understand how it works. I am assuming that if I increase my crystal speed to 20 mhz that I need to change my prescaler or am I way off? Can you explain how it works for me please? I have read the data sheet and understand how to adjust the registers I just don't know how to figure out what they should be.

DynamoBen
- 27th February 2006, 00:11
Sort of.

Check this piece of software out. It really helped me to understand the timers and interrupt times.

http://users.picbasic.org/projects/PicTmrCalc/PicTimerCal.htm

BTW: The datasheet does have a calculation in it. Honestly I never understood it.

Travin77
- 27th February 2006, 01:26
Thanks for the info but now I have other problems. While trying to compile the program in Microstudio using picbasic pro it gives me an error on this line:

Serout portb.2, N9600, [254,1]

I am trying to send this data out to the lcd which is serial. It gives me this error on every line with this command. I am using the code as before with the adjustments made. Here is a snipit of the top of the program, as I am sure this is the problem hopefully

' CONFIGURATION SETUP
' Oscillator: XT
' Watchdog Timer: OFF
' Power up Timer: OFF
' Master Clear Enable: Internal
' Brown Out Detect: OFF
' Code Protect: OFF
' Data EE Read Protect:OFF

'lcd setup
I con 254
CLR con 1
Line1 con 128
line2 con 192
line3 con 148
line4 con 212

DEFINE OSC 4


'variables
HzTimer VAR Word '1/2 second counter (2 Hz)
HH VAR Byte ' Hours 1-12
MM VAR ByTE ' Minutes 0-59
SS VAR Byte ' Seconds 0-59
X VAR Byte ' temp variable
col VAR Bit ' colon 1=on, 0=0ff

'initial conditions of controller
PORTB.0 = 0 'PORTB.0 PIN LOW
TRISB.O = 1 'PORTB.0 PIN input for button
OPTION_REG=%00000111 'weak pullups on, TMRO prescale = ‘256
HzTimer=$7A12 'for 1/2 Sec
HH=0:MM=0:SS=0:col=0 'initial conditions
Pause 1000 'settle Time for Serial LCD

' ************************************************** ************
'SET INITIAL TIME WITH PUSH BUTTON (PART 1 of Program)

Serout portb.2, N9600, [254,1] this and every other one like it give the same error "bad expression". What am I doing wrong? I am trying to compile the program. Thanks

DynamoBen
- 27th February 2006, 02:06
Boy I had to say this but...on page 137 of the manual it states:

The Mode names (e.g. T2400) are defined in the file MODEDEFS.BAS. To use them, add the line:

Include "modedefs.bas"

to the top of the PICBASIC PRO™ program.

Use your manual, its the Bible of PICBasic. I use mine so much that its falling apart. I also have the web version bookmarked.

Travin77
- 27th February 2006, 03:09
I noticed that a little later. One more question and I will leave you alone maybe =). I am trying to send this to the lcd controller:

SEROUT LCD,6,[I,LINE4,"FAN IS RUNNING"]

I am pretty sure the part "FAN IS RUNNING" is the problem, I just don't know how to fix it. I need this to appear on the lcd. Is the proper syntax?

DynamoBen
- 27th February 2006, 03:11
Short answer is it depends on the protocol of the LCD. Take a look at the LCD's info.

Travin77
- 27th February 2006, 03:44
Thanks for the help.