View previous topic :: View next topic |
Author |
Message |
Futterama
Joined: 17 Oct 2005 Posts: 98
|
PIC10F206 and printf/delay_ms problem |
Posted: Thu Nov 29, 2007 7:11 am |
|
|
Hello forum,
I have this small program:
.c file:
Code: | #include "main.h"
void main()
{
setup_comparator(NC_NC); // Disable comparator outputs
// Set the tristating for the IO port.
// TRIS is not a physical register, so it must be set using TRIS.
#asm
//Set the GP pins.
//GP0: output
//GP1: output
//GP2: output
//GP3: input
MOVLW 0b00000000
MOVWF GPIO
MOVLW 0b11111000
TRIS GPIO
// Set the OPTION register.
MOVLW 0b11000000
OPTION
#endasm
delay_ms(500);
printf("Test1");
}
|
.h file:
Code: | #include <10F206.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOPROTECT //Code not protected from reading
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B1,bits=8)
#byte GPIO = 0x06
#bit GP0 = GPIO.0
#bit GP1 = GPIO.1
#bit GP2 = GPIO.2
#bit GP3 = GPIO.3
#byte TMR0 = 0x01 |
When I use the line delay_ms(500); nothing is printed from the PIC. If I remove the line, the PIC will print "Test1".
Why does the delay_ms prevent the PIC from outputting the text?
The output pin is GP1. No external components except the RS-232 driver input is connected to the PIC.
Regards,
Futterama |
|
|
Ttelmah Guest
|
|
Posted: Thu Nov 29, 2007 8:16 am |
|
|
First, why the assembler?. The compiler is quite happy to handle the TRIS and option for you.
Have you tried loading the code into MPLAB, or looked at the fuse bits in your programmer? What you describe, would happen, if the watchdog was not disabled.
How is the RS232 receive connected?. To what pin?.
Have you got anything connected to the MCLR pin?. If not, try putting a resistor to one supply rail on this pin. The MCLR pin, even if disabled, can trigger a reset, if it sees a signal that goes outside the supply rail.
Best Wishes |
|
|
Futterama
Joined: 17 Oct 2005 Posts: 98
|
|
Posted: Thu Nov 29, 2007 8:30 am |
|
|
Ttelmah wrote: | First, why the assembler?. The compiler is quite happy to handle the TRIS and option for you.
|
Because there is a problem with the compiler handling the TRIS, I have read this in several threads - and of course I can't find these threads right now...
Ttelmah wrote: | Have you tried loading the code into MPLAB, or looked at the fuse bits in your programmer? What you describe, would happen, if the watchdog was not disabled. |
No, I guess the watchdog should be disabled because I've got the NOWDT fuse. My programmer (PICkit2) shows: "Configuration: 0008". I have no idea how to read the fuses, and I can't seem to find anything about fuses in the PIC10F206 datasheet. I'm not sure what you mean by loading the code into MPLAB.
Ttelmah wrote: | How is the RS232 receive connected?. To what pin?. |
I wrote this in my first post, pin GP2 and the pin i directly connected to the MAX232 input pin.
Ttelmah wrote: | Have you got anything connected to the MCLR pin? If not, try putting a resistor to one supply rail on this pin. The MCLR pin, even if disabled, can trigger a reset, if it sees a signal that goes outside the supply rail. |
Nothing connected here, I'll try the resistor.
Last edited by Futterama on Thu Nov 29, 2007 8:50 am; edited 2 times in total |
|
|
Futterama
Joined: 17 Oct 2005 Posts: 98
|
|
Posted: Thu Nov 29, 2007 8:38 am |
|
|
OK, I figured out the FUSES vs. Configuration Word vs. WDT issue. I didn't realise that the fuses was the same thing as the configuration word. The WDT IS disabled. |
|
|
Futterama
Joined: 17 Oct 2005 Posts: 98
|
|
Posted: Thu Nov 29, 2007 8:48 am |
|
|
Adding the resistor to the MCLR pin does not help. |
|
|
Futterama
Joined: 17 Oct 2005 Posts: 98
|
|
Posted: Thu Nov 29, 2007 9:01 am |
|
|
Ttelmah wrote: | First, why the assembler?. The compiler is quite happy to handle the TRIS and option for you. |
I tried to replace
Code: | MOVLW 0b00000000
MOVWF GPIO
MOVLW 0b11111000
TRIS GPIO
|
with
Code: | #use fast_io(b)
set_tris_b(0b11111000); |
and the asm code in the .lst file seems to be the same.
How do I set the OPTION register with C-code? |
|
|
Futterama
Joined: 17 Oct 2005 Posts: 98
|
|
Posted: Thu Nov 29, 2007 9:07 am |
|
|
By the way, If I output a char before the delay_ms(500); both that char and the chars after the delay will be output. I guess is must have something to do with the PC's UART timeout / startbit / delay from power up to first output char from PIC. |
|
|
Ttelmah Guest
|
|
Posted: Thu Nov 29, 2007 9:32 am |
|
|
The option register is set by the 'setup_wdt' function, so:
setup_wdt(DISABLE_PULLUPS|DISABLE_WAKEUP_ON_CHANGE);
will do what you currently do with this part of the assembler. I do remember there being some tris problems with these chips, but they were about 100 code releases ago!.
Try setting the serial output line high at the start of the code. I suspect you may have a problem because the line sits 'low', resulting in the PC seeing the pause as a 'break. So if you set pin B1 at the start of the code, it may fix the behaviour.
Best Wishes |
|
|
Futterama
Joined: 17 Oct 2005 Posts: 98
|
|
Posted: Thu Nov 29, 2007 9:57 am |
|
|
Looking at the .lst file, your suggestion seems to take a lot of code space, and in these devices, you should save codespace where possible...
Code: | .................... // Set the OPTION register.
.................... // Enable pin pull-ups.
.................... // Assign prescaler to Timer0 and set it to 1:16
.................... MOVLW 0b11000011
004C: MOVLW C3
.................... OPTION
004D: OPTION
.................... #endasm
....................
.................... setup_wdt(DISABLE_PULLUPS|DISABLE_WAKEUP_ON_CHANGE);
004E: MOVLW C0
004F: MOVWF 08
0050: MOVLW 07
0051: CLRF 01
0052: MOVLW 0F
0053: OPTION
0054: CLRWDT
0055: MOVF 08,W
0056: OPTION |
|
|
|
Ttelmah Guest
|
|
Posted: Thu Nov 29, 2007 10:14 am |
|
|
Normally the function simply takes the value that the constants combine to form, and do a single option instruction to set them. The reason it is doing so much work, is because you are already setting the counter value first
You'd want to use:
setup_counters(DISABLE_PULLUPS|DISABLE_WAKEUP_ON_CHANGE|RTCC_DIV_16);
Which should do everything in one hit.
Does setting the pin high, fix the RS232 problem?.
Best Wishes |
|
|
Futterama
Joined: 17 Oct 2005 Posts: 98
|
|
Posted: Thu Nov 29, 2007 1:17 pm |
|
|
setup_counters(DISABLE_PULLUPS|DISABLE_WAKEUP_ON_CHANGE|RTCC_DIV_16);
is not a valid syntax, it needs commas:
Syntax: setup_counters (rtcc_state, ps_state)
Also, the setup_wdt() is still doing lots of work without me setting the counter value first:
Code: | ....................
.................... setup_comparator(NC_NC); // Disable comparator outputs
0044: MOVLW 27
0045: MOVWF 07
0046: MOVF 00,W
0047: MOVLW 03
0048: MOVWF 08
0049: DECFSZ 08,F
004A: GOTO 049
004B: MOVF 07,W
....................
.................... setup_wdt(DISABLE_PULLUPS|DISABLE_WAKEUP_ON_CHANGE|RTCC_DIV_16);
004C: MOVLW C3
004D: MOVWF 08
004E: MOVLW 07
004F: CLRF 01
0050: MOVLW 0F
0051: OPTION
0052: CLRWDT
0053: MOVF 08,W
0054: OPTION
....................
.................... set_tris_b(0b11111000);
0055: MOVLW F8
0056: TRIS 6
.................... |
I can see the setup_comparator() is also doing lots of work, that's why I usually just read the datasheet and do things myself (in C, I'm not very familiar with ASM). I seldom use the builtin functions for setup - reading the datasheet is also a good way to learn how things work.
Ttelmah wrote: | Does setting the pin high, fix the RS232 problem? | I don't know, for some reason I can reproduce the error now... |
|
|
|