|
|
View previous topic :: View next topic |
Author |
Message |
ViktorP Guest
|
Cannot write to TRIS A |
Posted: Wed Oct 31, 2007 4:30 pm |
|
|
I'm just playing with PICkit1, where are 8 LEDs addressed with 4 ports (A1, A2, A4, A5). Look at this silly example:
Code: |
#include "leds.h"
#use fast_io(A)
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_comparator(NC_NC);
setup_vref(FALSE);
while(1) {
set_tris_a(0b00001110); //set TRISA to 0x0E - OK
output_a(0b00010000);
delay_ms(50);
set_tris_a(0b00111000); //set TRISA to 0x08 - WHY!?
output_a(0b00000010);
delay_ms(50);
}
}
|
The program should blink with two LEDs, but it does not. I tried to debug it with MPLab SIM debugger and it really never set TRISA high nibble to anything but 0b0000. It is same even when I try to overwrite TRISA manually in Special function register watch. I write 0xFFh and the field promptly change to 0x0Fh. I tried MPUs 16F688 and 16F676.
When I extract assembler code from C/ASM list in CCS IDE and compile it in MPLab it works like a charm. I really have no idea why?
It probably will be problem in some FUSES or something like this (I'm quite newie). This is header file:
Code: |
#include <16F676.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES LP //Low power osc < 200 khz
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOPUT //No Power Up Timer
#FUSES BANDGAP_HIGH
#use delay(clock=20000000)
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 31, 2007 4:47 pm |
|
|
Here's the User's Guide:
http://ww1.microchip.com/downloads/en/DeviceDoc/40051D.pdf
Look in this section:
Quote: | 5.2 FAQS
5.2.1 Program Does Not Work
My program does not work. What should I check?
Answer:
When using the PICkit 1 Flash Starter Kit’s on-board
evaluation socket, configure the internal oscillator. |
To use the internal oscillator, setup your #fuses and #use delay()
statement as follows:
Code: | #include <16F688.h>
#fuses INTRC_IO, NOWDT, NOBROWNOUT, PUT, NOMCLR
#use delay(clock=8000000) |
|
|
|
Guest
|
|
Posted: Thu Nov 01, 2007 7:56 am |
|
|
Thank you for your answer. I haven't known that. But it didn't help me :( I tried all fuses which are used for RC, but with no success. Actualy, my program works. It is blinking with LEDs. But chaotically (of course not exactly). Still cannot change TRISA bits 5 and 6.
When I try it in ASM, it works fine. The problem will probably be in FUSES, but I don't know, how to "translate" MPLab fuses to CCS fuses. In ASM (when using P16F688) I have:
Code: |
_CONFIG _CP_OFF & _CPD_OFF & _BOD_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _FCMEN_OFF & _IESO_OFF
|
Do somebody knows, how should theese FUSES look in CCS compiler? |
|
|
Ttelmah Guest
|
|
Posted: Thu Nov 01, 2007 8:14 am |
|
|
What makes you think you 'cannot set tris bits'?.
Your code is running in 'standard_io' mode. This is the default. Hence, your TRIS bits will be set as you show, and on the very next program line, will be _overridden_. In standard_io mode, the tris bits are set automatically, whenever you perform I/O. You output an entire byte to the port, so the TRIS register will be set to '0b00000000' as soon as you do this. So your code, sets the TRIS to one pattern, and then immediately has it changed.
If you want to manually control TRIS, then switch to using 'fast_io' mode, which means that _you_ have to control the TRIS.
Best Wishes |
|
|
ViktorP Guest
|
|
Posted: Thu Nov 01, 2007 3:54 pm |
|
|
Oh, thats probably THAT problem. I have thought (from CCS manual) that when I use
then I'm using 'fast_io' mode you are talking about.
How should I configure compiler to use fast IO mode when '#use fast_io(A)' is not enough?
In sentence 'cannot set tris bits' I mean, that when I start debugger (MPLab SIM), then I cannot manualy change upper nibble of TRISA SFR. Even when I'm on first line of code (PCL points at 0x00h). Neither can program itself. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 01, 2007 4:19 pm |
|
|
I made a test program and compiled it with vs. 4.059. I ran it in MPSIM
in MPLAB vs. 7.41. I set breakpoints on the three delay_cycles(1)
statements in the code below. I setup a Watch Window to watch the
TRISA register. It gave the results shown below in the comments.
They are correct. According to the 16F676 data sheet, bits 6 and 7
will always read as 0, and bit 3 will always read as 1.
Code: |
#include <16F676.H>
#fuses INTRC_IO, NOWDT, NOBROWNOUT, PUT, NOMCLR
#use delay(clock=8000000)
//======================================
void main()
{
set_tris_a(0x0F);
delay_cycles(1); // TRISA = 0x0F
set_tris_a(0xF0);
delay_cycles(1); // TRISA = 0x38
set_tris_a(0x0F);
delay_cycles(1); // TRISA = 0x0F
while(1);
} |
|
|
|
ViktorP Guest
|
|
Posted: Fri Nov 02, 2007 8:54 am |
|
|
Dear 'PCM Programmer' IT IS WORKING!!!
Thank you very much as this problem has been making my head to headache last four days. Your program, as you wrote it, really works perfectly. So I tried to figure out why it had not been working when you advised me before. The trick was in using fuse INTRC_IO. But I used this fuse with others fuses (generated by CCS wizard). And problem was in using fuse LP (Low power osc < 200 kHz). Today, when I disabled this fuse all started to work.
I'm sorry I don't know, what internal oscillator is and what 'LP' means. Of course I understand, that something must dictate heardbeat for processor to execute instructions but as I'm only machine engineer I don't know details about which oscillators are on PICkit board. Of course it is my problem.
Once again I thank you very much for time you spent with my problem. Now I can continue in my diploma work. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|