View previous topic :: View next topic |
Author |
Message |
Goodwinchang
Joined: 15 Oct 2006 Posts: 18 Location: Taiwan
|
PIC12F683 with RS232 and RTC clock application |
Posted: Fri Nov 10, 2006 12:44 am |
|
|
Dear all
Now I have a problem when I want to implement an application using PIC12F683 to have RS232 and RTC function. But My RS232 can't get any input from my PIC.
#include "F:\Download\Working\CCS\PIC12F683\11102006001\Rs232.h"
#include <stdlib.h>
#define Timecount 170
int RTC_count ;
int save_w;
#locate save_w=0x7E
int save_status;
#locate save_status=0x7F
#byte status = 0x03
#bit zero_flag = status.2
#bit t0if = 0x8b.2
int RTC_SEC, RTC_MIN, RTC_HOUR ;
int RTC_DAY, RTC_MON, RTC_YEAR ;
int1 INC_DAY ;
#int_TIMER2
TIMER2_isr()
{
#asm
//store current state of processor
MOVWF save_w
SWAPF status ,W
BCF status ,5
BCF status ,6
MOVWF save_status
#endasm
if (!(--RTC_count))
{
if((++RTC_SEC) == 60)
{
if((++RTC_MIN) == 60)
{
if ((++RTC_HOUR) == 24)
{
RTC_HOUR = 0 ;
INC_DAY = 1 ;
}
RTC_MIN = 0 ;
}
RTC_SEC = 0 ;
}
RTC_count = Timecount ;
output_toggle(PIN_A0) ;
}
#asm
SWAPF save_status,W
MOVWF status
SWAPF save_w,F
SWAPF save_w,W
#endasm
}
char inputchar ;
char stringaa[20] ;
char tempstr[2];
int test ;
void main()
{
set_tris_a(0x34) ;
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_16,194,16);
setup_comparator(NC_NC);
setup_vref(FALSE);
RTC_count = Timecount ;
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
setup_oscillator(False);
INC_DAY = 0;
// TODO: USER CODE!!
while(1) {
fputs("test ", ABC) ;
output_toggle(PIN_A1) ;}
}
Where is the problem? Thanks for help.
Goodwin |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 10, 2006 12:55 am |
|
|
Quote: | #asm
//store current state of processor
MOVWF save_w
SWAPF status ,W
BCF status ,5
BCF status ,6
MOVWF save_status
#endasm
etc. |
From the CCS manual, pages 95 and 96:
Quote: |
#INT_TIMER2:
The compiler will generate code to jump to the function when the
interrupt is detected. It will generate code to save and restore the
machine state, and will clear the interrupt flag. |
|
|
|
Goodwinchang
Joined: 15 Oct 2006 Posts: 18 Location: Taiwan
|
|
Posted: Fri Nov 10, 2006 2:45 am |
|
|
Actually I don't really understand what your means. Do you want me to delete the ASM portion code? There should be some problems but the key issue is my RS232. It doesn't work.
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 10, 2006 2:50 am |
|
|
Post the following statements in your program:
1. #fuses
2. #use delay()
3. #use rs232() |
|
|
Goodwinchang
Joined: 15 Oct 2006 Posts: 18 Location: Taiwan
|
|
Posted: Fri Nov 10, 2006 3:09 am |
|
|
#include <12F683.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOCPD //No EE protection
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOMCLR //Master Clear pin enabled
#FUSES NOPUT //No Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES NOFCMEN //Fail-safe clock monitor enabled
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_A3,rcv=PIN_A2,bits=8,stream=ABC) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 10, 2006 3:31 am |
|
|
A few more questions:
1. Are you still using vs. 4.012 of the compiler ?
2. Have you downloaded and tried to use vs. 3.249 ?
3. Do you have a 20 MHz crystal connected to the 12F683 ?
(with the appropriate capacitors).
4. Are you using a MAX232 chip between the 12F683 and the PC ?
5. Have you tried to make RS232 work with a simple program ?
(A program that just transmits "Hello World" to the PC, and does
not have all the interrupt code).
When I get into the company, I'll check if I have a 12F683.
If so, I'll test it with vs. 3.249 in hardware. |
|
|
Ttelmah Guest
|
|
Posted: Fri Nov 10, 2006 3:45 am |
|
|
Goodwinchang wrote: | Actually I don't really understand what your means. Do you want me to delete the ASM portion code? There should be some problems but the key issue is my RS232. It doesn't work.
Thanks. |
Yes.
The assembler is not needed, and wouldn't be enough anyway if it was (there are a lot more registers than this that need saving in C). The compiler already adds the code to save/restore the registers, so you are just wasting time in the interrupt, doing it again.
I really would suspect though, that the timer is a 'red herring'. Start by checking that the chip is running, and running at the frequency expected. A 'few liner', like:
[code]
void main() {
//Don't do this. Unless you specify 'fast_io',
//the compiler controls the TRIS
//set_tris_a(0x34) ;
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_16,194,16);
setup_comparator(NC_NC);
setup_vref(FALSE);
while (true) {
output_toggle(PIN_A0);
delay_ms(500);
}
}
Does the pin toggle?. Is the cycle time about 1 second?. If it doesn't toggle at all, then the odds are that the chip is not running. Are you sure your oscillator is working?. If the period is wrong, then look at the frequencies...
Only once this is working, and at the right frequency, satrt worrying about the RS232.
Best Wishes |
|
|
Goodwinchang
Joined: 15 Oct 2006 Posts: 18 Location: Taiwan
|
|
Posted: Fri Nov 10, 2006 4:22 am |
|
|
Thanks for your reply.
I have checked the output and the PIN_A1 is about 14mS and the oscillator is 20MHz. What I can do? |
|
|
Ttelmah Guest
|
|
Posted: Fri Nov 10, 2006 6:10 am |
|
|
If you are saying that this is the time taken in the original code, to toggle A1, then your oscillator is_not_ at 20MHz.
Is this the pulse width, or the repetition frequency?
If the pulse width is about 14mSec, then this corresponds to 40 bit times (four characters, each eight bits, plus one start bit each, and one stop bit each). This implies the baud rate is about 1/(14E-3/40). About 2900bps, which implies the oscillator is running at about 3.3MHz. If this is the repetition frequency, then the oscillator is running at about 6.6MHz.
What are you using as an oscillator?. Have you got the right capacitors fitted?.
Best Wishes |
|
|
adrian
Joined: 08 Sep 2003 Posts: 92 Location: Glasgow, UK
|
|
Posted: Fri Nov 10, 2006 6:39 am |
|
|
The 12F683 can do a software RS232. I am presently developing an application using v3.246 and it does work.
However, you are never going to get RS232 with this set-up.
Quote: | #use rs232(baud=9600,parity=N,xmit=PIN_A3,rcv=PIN_A2,bits=8,stream=ABC) |
Look again at the data sheet, and see what the internal construction of pin A3 looks like |
|
|
Goodwinchang
Joined: 15 Oct 2006 Posts: 18 Location: Taiwan
|
|
Posted: Fri Nov 10, 2006 8:02 pm |
|
|
Dear all
Who can provide me the working code for my reference?
Becuase I have using 3.249 version and it still doesn't work.
My current status is the output toggle is working correct and the Timer is also work properly. Only the RS232 output still work uncorrectly.
Best Regards
Goodwin |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 10, 2006 8:21 pm |
|
|
I didn't do any more work on it because Adrian identified the problem.
Adrian said:
Quote: |
Look again at the data sheet, and see what the internal construction of pin A3 looks like |
Hint: Pin A3 can't be used for the "xmit" pin. Choose a different pin
for "xmit" in your #use rs232() statement. |
|
|
|