CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PIC16f877A and timer0
Goto page Previous  1, 2, 3, 4
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
wind88



Joined: 29 Dec 2010
Posts: 37

View user's profile Send private message

PostPosted: Fri Jan 14, 2011 10:49 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jan 15, 2011 3:19 am     Reply with quote

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).
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4
Page 4 of 4

 
Jump to:  
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