|
|
View previous topic :: View next topic |
Author |
Message |
ntrungtruc2003
Joined: 16 Feb 2011 Posts: 42
|
problem with input(PIN_B1) PIC18F2480 |
Posted: Wed Oct 11, 2017 9:42 am |
|
|
Dear Sir.
I want to test a simple command input(PIN_B1) from PIC18F2480, but I can not change the state input PIN_B1.
I have already configured fast_io(b), set_tris(0x03) // B0, B1 is input
PIN_B0, PIN_B1 is connected 10k resistor.
Compiler is V5.025
Code: |
#include <18F2480.h>
//#include <18F248.h>
#fuses HS,NOPROTECT,NOLVP,NOWDT //#fuses XT,NOWDT,NOPROTECT,NOLVP,PUT,BROWNOUT
#device 18F2480*=16, ADC=8 // ADC 10 bits
//#device HIGH_INTS=true
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use fast_io (b)
#include <LCD_TM_S.c>
void main ()
{
//setup_timer_2(T2_DIV_BY_4,79,16); //setup up timer2 to interrupt every 1ms if using 20Mhz clock
//enable_interrupts(INT_EXT1);
// enable_interrupts(INT_TIMER2); //enable timer2 interrupt
// clear_interrupt(INT_EXT1);
// EXT_INT_EDGE(0,H_TO_L);
//enable_interrupts(INT_EXT1);// B0
//enable_interrupts(INT_EXT_L2H);// B0
enable_interrupts(GLOBAL); //enable all interrupts (else timer2 wont happen)
// enable_interrupts(INT_SSP);
// enable_interrupts(INT_EXT1_L2H);// B1
setup_adc(adc_clock_internal);
setup_adc_ports(ALL_ANALOG); //ALL_ANALOG
// setup_ccp1(CCP_PWM); // PIN_C2 AS CCP PWM
lcd_init();
printf("\r\nRunning...");
// output_float(PIN_B0); //Makes pin B3 Input
set_tris_b(0x03);
// output_float(PIN_B1);
// output_bit(PIN_B0,1);
//port_b_pullups (TRUE);
delay_us (100); // Allow time for weak pullups to rise up
byte temp=input_b();
while (TRUE) {
if(input(PIN_B1)==1)
{
lcd_gotoxy(1,1);
printf(lcd_putc,"A%x",temp);
}
else
{
lcd_gotoxy(1,1);
printf(lcd_putc,"B%x", temp);
}
delay_ms(500);
}
}
|
I have also checked with .asm, but can not see TRIS command in this.
Code: |
.................... set_tris_b(0x03);
0276: MOVLW 03
0278: MOVWF F93
|
Could you please teach me how can i solve this.
Thank you very much. |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Wed Oct 11, 2017 10:17 am |
|
|
Haven't check the datasheet but if B0 & B1 are analog ports then you have set them to analog by using the
setup_adc_ports(ALL_ANALOG);
Regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Wed Oct 11, 2017 11:52 am |
|
|
Also as a comment, read the data sheet. ADC_CLOCK_INTERNAL is _not_ recommended for any clock rate above 1MHz, unless you are putting the chip to sleep for the conversion. The data sheet has a nice little table showing what clock you should use typically for different crystal rates.
and:
Code: |
.................... set_tris_b(0x03);
0276: MOVLW 03
0278: MOVWF F93
|
is exactly the tris command. Register 0xF93, is the TRISB register. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 11, 2017 6:58 pm |
|
|
Quote: |
I have also checked with .asm, but can not see TRIS command in this.
.................... set_tris_b(0x03);
0276: MOVLW 03
0278: MOVWF F93
|
I think he means the TRIS instruction. But there is no TRIS instruction
listed in the 18F2480 data sheet. That's because Microchip started phasing
it out with the 16F877. From the 16F877 data sheet:
Quote: |
Note: To maintain upward compatibility with
future PIC16F87X products, do not use
the OPTION and TRIS instructions.
|
|
|
|
ntrungtruc2003
Joined: 16 Feb 2011 Posts: 42
|
The problem is solved |
Posted: Thu Oct 12, 2017 7:47 am |
|
|
Thanks everybody in here.
- The problem is PIC18F2480 have Analog input PIN_B0, PIN_B1, therefore just config: Code: | setup_adc_ports(AN0_TO_AN2); | the problem is solved.
- I have also modified external clock for adc like Ttelmah suggested Code: | setup_adc(ADC_CLOCK_DIV_16); | ,and the result for adc is better
- I have also configured 2 external interrupt on PIN_B0, PIN_B1
All things work.
Code: |
#include <18F2480.h>
//#include <18F248.h>
#fuses HS,NOPROTECT,NOLVP,NOWDT //#fuses XT,NOWDT,NOPROTECT,NOLVP,PUT,BROWNOUT
#device 18F2480*=16, ADC=8 // ADC 10 bits
#device HIGH_INTS=true
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <LCD_TM_S.c>
int C, C1;
#INT_EXT HIGH
void ext1_isr(void) {
output_bit(PIN_C5,1);
C1++;
}
#INT_EXT1
void ext1_isr1(void) {
output_bit(PIN_C5,0);
C++;
}
void main ()
{
//setup_timer_2(T2_DIV_BY_4,79,16); //setup up timer2 to interrupt every 1ms if using 20Mhz clock
// enable_interrupts(INT_TIMER2); //enable timer2 interrupt
EXT_INT_EDGE(0,H_TO_L);
clear_interrupt(INT_EXT);
enable_interrupts(INT_EXT);// B0
EXT_INT_EDGE(1,H_TO_L);
clear_interrupt(INT_EXT1);
enable_interrupts(INT_EXT1);// B0
enable_interrupts(GLOBAL); //enable all interrupts (else timer2 wont happen)
setup_adc(ADC_CLOCK_DIV_16);
setup_adc_ports(AN0_TO_AN2); //ALL_ANALOG
lcd_init();
printf("\r\nRunning...");
set_tris_b(0x03);
delay_us (100); // Allow time for weak pullups to rise up
int32 temp=input_b();
int32 A2;
output_bit(PIN_B0,1);
while (TRUE) {
if(C>4){
C=0;}
if(C1>4){
C1=0;}
lcd_gotoxy(1,1);
printf(lcd_putc,"A%x",C);
set_adc_channel(2); // EasyPIC Board: Read ADC 2 for PWM fan
delay_ms(20);
A2= read_adc();
lcd_gotoxy(10,1);
printf(lcd_putc,"A2%LU",A2);
lcd_gotoxy(1,2);
printf(lcd_putc,"A%x",C1);
delay_ms(500);
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Thu Oct 12, 2017 11:09 am |
|
|
Well done. |
|
|
|
|
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
|