View previous topic :: View next topic |
Author |
Message |
Noland
Joined: 11 Dec 2011 Posts: 11 Location: Thailand
|
Test GPIO 16F1823 |
Posted: Sun Dec 11, 2011 10:05 am |
|
|
Hello. I'm Palm in writing codes at CCS C. PIC 16f1823, I can't control PIC16f1823 is GPIO. How to set up 16f1826 GPIO ?
Code: | #include <16F1823.H>
#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR
#use delay(clock=8M)
#use fast_io(A)
#byte PORTA = 0x00C
#byte TRISA = 0x08C
#byte ANSELA = 0x18C
#byte LATA = 0x10C
void main()
{
setup_oscillator(OSC_8MHZ|OSC_PLL_ON);
//setup_oscillator(OSC_8MHZ | OSC_NORMAL | OSC_PLL_ON);
ANSELA = 0x00;
TRISA = 0b00000111;
while(TRUE)
{
LATA = 0b00000000;
delay_ms(1600);
LATA = 0b00000111;
delay_ms(1600);
}
} |
and
Code: | #include <16F1823.H>
#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR
#use delay(clock=8M)
#use fast_io(A)
#byte PORTA = 0x00C
#byte TRISA = 0x08C
#byte ANSELA = 0x18C
#byte LATA = 0x10C
void main()
{
setup_oscillator(OSC_8MHZ|OSC_PLL_ON);
set_tris_A (0x08);
ANSELA = 0x00;
TRISA = 0b00000000;
while(TRUE)
{
output_a(0x00);
delay_ms(1600);
output_a(0xff);
delay_ms(1600);
}
} |
This codes don't work |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Sun Dec 11, 2011 10:49 am |
|
|
Seriously, just use the compiler!.
The tris register is already available for your without any register defines, so it the latch register.
Code: |
#include <16F1823.H>
#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR
#use delay(clock=8M)
#use fast_io(A)
void main(void) {
setup_oscillator(OSC_8MHZ|OSC_INTRC); //setting for 8MHz
// - if you want the
//PLL change the clock to match, or the timings will be wrong.
set_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
set_tris_a(0b00000111);
while(TRUE) {
output_a(0b00000000);
delay_ms(1600);
output_a(0b00000111);
delay_ms(1600);
}
}
|
If you want to write assembler, use assembler, but with the compiler just use the compiler functions unless you _must_ work directly.
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Dec 11, 2011 1:23 pm |
|
|
If you can't make it work with CCS functions, then post your compiler version. |
|
|
Noland
Joined: 11 Dec 2011 Posts: 11 Location: Thailand
|
|
Posted: Sun Dec 11, 2011 8:59 pm |
|
|
CCS V.4.107 Now, don't work and All Pin PortA output 2.4V stable |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 12, 2011 1:16 am |
|
|
I'll test it in hardware, tomorrow morning with your version. |
|
|
Noland
Joined: 11 Dec 2011 Posts: 11 Location: Thailand
|
|
Posted: Mon Dec 12, 2011 3:31 am |
|
|
I test...
PIC Work, If set portA output_high or output_low
PIC don't work, If set portA output_high output_low Alternate output at portA is 2.4V stable
I think it's set x-tal Wrong.
I trial Ext and INT x-tal |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 12, 2011 1:32 pm |
|
|
Quote: | If set portA output_high output_low Alternate output at portA is 2.4V stable |
How are you testing this ? Are you using an oscilloscope or a voltmeter ?
Because if you are using a voltmeter, it might be averaging the alternating
waveform into the average voltage. If goes between 0v and 4.8v,
equally for each half-cycle, the average will be 2.4 volts. Is that what
you are doing ? Are you using a voltmeter ? |
|
|
Noland
Joined: 11 Dec 2011 Posts: 11 Location: Thailand
|
|
Posted: Mon Dec 12, 2011 7:40 pm |
|
|
Code: |
output_a(0xff)
delay_ms(2000);
output_a(0x00)
delay_ms(2000); |
using a voltmeter Because using delay 2000 ms and test at delay 10000 ms pic don't work
the output should be 0-4.8V (0 1 0 1....)
but the output is 2.4V stable |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 12, 2011 8:36 pm |
|
|
Unfortunately I had to be out of the office almost all day (unexpectedly).
I believe I will be able to test it in hardware on Tuesday morning. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Dec 13, 2011 1:23 pm |
|
|
Quote: | CCS V.4.107 Now, don't work and All Pin PortA output 2.4V stable |
I looked at the .LST file and tested it in hardware.
Vs. 4.107 is just terrible for the 16F1823. It's missing half the fuses.
The .h file only lists some of the actual fuses in the PIC. The compiler
doesn't use the 2nd Config Word. It doesn't really know that it exists.
The delay_ms() routine doesn't work. It just returns quickly, maybe
doing a delay of a little more than 10 usec, when you call delay_ms(1).
There are so many things wrong that I don't want to try to make
work-arounds for all the bugs in vs. 4.107 for the 16F1823.
It would be better if you upgraded the compiler.
Quote: | the output should be 0-4.8V (0 1 0 1....)
but the output is 2.4V stable |
I think the reason for this is because the waveform runs at 30 KHz or
higher (because of the bad delay_ms code), so the voltmeter just
averages it to 2.4v.
I'll do one work-around:
I've done a very quick replacement for delay_ms() which will work
for 4 MHz oscillator frequency. It uses delay_cycles(), which takes
1 usec at 4 MHz, so delay_cycles(250) gives a delay of 250 usec.
4 x 250 = 1000 usec = 1 ms delay. When the work-around function
is written as a macro (with a #define statement) then it will replace
the existing built-in function, as shown below:
Code: |
#include <16F1823.H>
#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR
#use delay(clock=4M)
int16 cnt;
#define delay_ms(x) \
cnt = x; \
while(cnt) \
{ \
delay_cycles(250); \
delay_cycles(250); \
delay_cycles(250); \
delay_cycles(250); \
cnt--; \
} \
//========================================
void main()
{
while(1)
{
output_a(0x00);
delay_ms(1600);
output_a(0xff);
delay_ms(1600);
}
}
|
|
|
|
Noland
Joined: 11 Dec 2011 Posts: 11 Location: Thailand
|
|
Posted: Tue Dec 13, 2011 10:17 pm |
|
|
Thank you very much. now pic work |
|
|
Noland
Joined: 11 Dec 2011 Posts: 11 Location: Thailand
|
|
Posted: Fri Dec 16, 2011 2:20 am |
|
|
If use XTAL=8MHz PLLx4 = 32MHz I will write delay_cycles(??); ??.
Code: |
#include <16F1823.H>
#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR
#use delay(clock=8M)
int16 cnt;
#define delay_ms(x) \
cnt = x; \
while(cnt) \
{ \
delay_cycles(??); \
delay_cycles(??); \
delay_cycles(??); \
delay_cycles(??); \
cnt--; \
} \
//========================================
void main()
{
setup_oscillator(OSC_8MHZ|OSC_PLL_ON);
while(1)
{
output_a(0x00);
delay_ms(1600);
output_a(0xff);
delay_ms(1600);
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 16, 2011 1:07 pm |
|
|
This is one way to do it. Because 32 MHz is 8x faster than 4 MHz, just
put in a for() loop that calls the same delay block for 8x.
Code: |
#include <16F1823.H>
#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR
#use delay(clock=32M)
int8 i;
int16 cnt;
#define delay_ms(x) \
cnt = x; \
while(cnt) \
{ \
for(i=0; i<8; i++) \
{ \
delay_cycles(250); \
delay_cycles(250); \
delay_cycles(250); \
delay_cycles(250); \
} \
cnt--; \
}
//========================================
void main()
{
while(1)
{
output_a(0x00);
delay_ms(1600);
output_a(0xff);
delay_ms(1600);
}
}
|
If you need any more work-arounds for this compiler version with this
PIC, you need to learn how to do it. |
|
|
|