thiam
Joined: 06 Apr 2010 Posts: 13
|
have trouble with PIC18F4331 in receive signal from encoder |
Posted: Fri Apr 09, 2010 7:29 am |
|
|
hi, I have a trouble in develop PIC18F4331 in receive signal from rotary encoder. The channel phase A and B are used only. Below is the sample code and circuit for PIC16F877A which has build external decode.
Code: |
#include <16F877A.h>
#include <string.h>
#use delay(clock=20000000)
#fuses hs, noprotect, nowdt,nolvp
#byte PORTB=6
#byte PORTC=7
#define door PIN_B2
#define window PIN_B1
#define bumper PIN_B3
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define REDLED PIN_B4
#define GREENLED PIN_B6
#define YELLOWLED PIN_B3
#define DIRECTION PIN_D7
#include <input.c>
#include <stdlib.h>
#define STRING_SIZE 40
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
#define bkbhit (next_in!=next_out)
long t0 = 0;
long t1 = 1;
byte bgetc()
{
byte c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
//!
void get_cmd_str(char * s, int max) {
int len;
char c;
max--;
len = 0;
do {
c = bgetc();
if(c > 0x00) {
if(len < max) {
s[len++] = c;
}
}
}
while(bkbhit);
s[len]= 0x00;
}
#int_rda
void serial_received()
{
int t;
buffer[next_in]=getc();
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
#int_ext
void interrupt()
{
BYTE time3;
set_tris_b(0b00000001);
output_high(bumper);
time3 = get_timer0();
time3 += -1;
set_timer0(time3);
}
#int_rtcc
void timer0_isr()
{
t0 += 1;
}
#int_timer1
void timer1_isr()
{
t1 += 1;
set_timer1(0xFF00);
}
void main()
{
setup_timer_2(T2_DIV_BY_16, 127, 1);
setup_counters( RTCC_EXT_H_TO_L, RTCC_DIV_1 );
set_rtcc(0);
setup_timer_1 ( T1_EXTERNAL | T1_DIV_BY_1 );
set_timer1(0xFFF4);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_TIMER1);
EXT_INT_EDGE(L_TO_H);
ENABLE_INTERRUPTS(INT_EXT);
ENABLE_INTERRUPTS(INT_RDA);
enable_interrupts(GLOBAL);
main_program:
do
{
printf("%2x",t0);
printf("%2x",get_rtcc());
printf("%2x",t1);
printf("%2x\n\r",get_timer1());
//!
output_high(GREENLED);
output_low(REDLED);
output_low(YELLOWLED);
}while(TRUE);
goto main_program;
}
|
http://www.flickr.com/photos/49158679@N02/4505396788/
Now, My problem is no idea to write the code in PIC18F4431.. Anyone can guide me about that?? thanks |
|