|
|
View previous topic :: View next topic |
Author |
Message |
wind88
Joined: 29 Dec 2010 Posts: 37
|
|
Posted: Fri Jan 14, 2011 10:49 pm |
|
|
thank you Ttelmah
Code: | #include <16F877A.h>
#device adc=10
#use delay(clock=20000000)
#define XTAL_FREQ 200000000
#include <flex_lcd.c>
#FUSES HS, NOWDT,NOPROTECT,NOLVP,NOWDT,PUT,NOBROWNOUT
#include <stdio.h>
#define MAX_VALUE 200
#define CCW_ROTATION MAX_VALUE - 25
#define CW_ROTATION MAX_VALUE - 15
#define STOP_ROTATION MAX_VALUE
#define THRESHOLD_VALUE 25
#define A0 PIN_A0
#define A1 PIN_A1
#define B1 PIN_B1
unsigned char pulse_max=0;
unsigned char pulse_top=0;
unsigned char top_value=0 ;
#INT_TIMER0
void TIMER0_isr(void)
{
if(INT_TIMER0)// TIMER0 Interrupt Flag
{
pulse_max++; // Pulse Max Increment
pulse_top++; // Pulse Top Increment
/* MAX_VALUE=200 turn off the pulse */
if (pulse_max >= MAX_VALUE) {
pulse_max=0;
pulse_top=0;
output_low(B1); // Turn off RB1
}
if (pulse_top == top_value) {
output_high(B1); // Turn On RB1
}
}
}
void main(void)
{
signed int16 ldr_left=0 ;
signed int16 ldr_right=0 ;
signed int16 ldr_diff ;
port_b_pullups(TRUE);
setup_adc_ports(ALL_ANALOG );
setup_adc(ADC_CLOCK_DIV_32);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
lcd_init();
// Init Servo Pulse
pulse_max=0;
pulse_top=0;
top_value = MAX_VALUE; // top_value = MAX_VALUE: Servo Motor Stop
// Init TIMER0: Period: Fosc/4 x Prescaler x TMR0
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
// Read the ADC here
while(1){
set_adc_channel(0);
delay_us(10);
ldr_left=read_adc();
printf(lcd_putc, "\f value = %ld \n",ldr_left);
set_adc_channel(1);
delay_us(10);
ldr_right=read_adc();
printf(lcd_putc, "value = %ld ",ldr_right);
// Get the different
ldr_diff = ldr_left - ldr_right;
if ((ldr_diff >= -THRESHOLD_VALUE) && (ldr_diff <= THRESHOLD_VALUE))
{
output_low(B1); // Stop the Servo Motor
}
else
{
if (ldr_diff > THRESHOLD_VALUE)
{
top_value = CCW_ROTATION; // Counterclockwise Rotation
}
else
{
top_value = CW_ROTATION; // Clockwise Rotation
}
}
}
}
|
This code cant turn on the lcd also.
Do you mean that is something is wrong in the timer0 there? |
|
|
wind88
Joined: 29 Dec 2010 Posts: 37
|
|
Posted: Sat Jan 15, 2011 3:19 am |
|
|
Quote: | #include <16F877A.h>
#device adc=10
#use delay(clock=20000000)
#define XTAL_FREQ 200000000
#include <flex_lcd.c>
#FUSES HS, NOWDT,NOPROTECT,NOLVP,NOWDT,PUT,BROWNOUT
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_VALUE 200
#define CCW_ROTATION MAX_VALUE - 25
#define CW_ROTATION MAX_VALUE - 15
#define STOP_ROTATION MAX_VALUE
#define THRESHOLD_VALUE 25
#define A0 PIN_A0
#define A1 PIN_A1
#define B1 PIN_B1
unsigned char pulse_max=0;
unsigned char pulse_top=0;
unsigned char top_value=0 ;
#INT_TIMER0
void TIMER0_isr(void)
{
if(INT_TIMER0)// TIMER0 Interrupt Flag
{
pulse_max++; // Pulse Max Increment
pulse_top++; // Pulse Top Increment
/* MAX_VALUE=200 turn off the pulse */
if (pulse_max >= MAX_VALUE) {
pulse_max=0;
pulse_top=0;
output_low(B1); // Turn off RB1
}
if (pulse_top == top_value) {
output_high(B1); // Turn On RB1
}
}
}
void main(void)
{
signed int16 ldr_left=0 ;
signed int16 ldr_right=0 ;
signed int16 ldr_diff ;
port_b_pullups(TRUE);
lcd_init();
printf(lcd_putc, "\f value = %ld \n",ldr_left);
printf(lcd_putc, "value = %ld ",ldr_right);
setup_adc_ports(ALL_ANALOG );
setup_adc(ADC_CLOCK_DIV_32);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_2);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
// Init Servo Pulse
pulse_max=0;
pulse_top=0;
top_value = MAX_VALUE; // top_value = MAX_VALUE: Servo Motor Stop
// Init TIMER0: Period: Fosc/4 x Prescaler x TMR0
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
// Read the ADC here
while(true){
set_adc_channel(0);
delay_us(10);
ldr_left=read_adc();
//printf(lcd_putc, "\f value = %ld \n",ldr_left);
set_adc_channel(1);
delay_us(10);
ldr_right=read_adc();
//printf(lcd_putc, "value = %ld ",ldr_right);
// Get the different
ldr_diff = ldr_left - ldr_right;
if ((ldr_diff >= -THRESHOLD_VALUE) && (ldr_diff <= THRESHOLD_VALUE))
{
output_low(B1); // Stop the Servo Motor
}
else
{
if (ldr_diff > THRESHOLD_VALUE)
{
top_value = CCW_ROTATION; // Counterclockwise Rotation
}
else
{
top_value = CW_ROTATION; // Clockwise Rotation
}
}
}
}
|
If I put the printf after the lcd_init();
the value is shown in the screen but it only show value=0 (I think it is wrong since the value is taken before the conversion). |
|
|
|
|
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
|