void interrupt isr(void)
{
if(TMR0IF) // TMR0 is overflow
{
TMR0IF = 0; // clear flag bit
To += 0x100; // count number of TMR0 overflow ( make it to 16bit TMR)
sysclock++;
}
}
void init()
{
TRISB = 0b00000110; //set RB2 pin as input, other as output
TRISC = 0b00000000; //set PORTA as output
TRISD = 0b00000000;
ADCON1 = 0x80; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
RD1=1;
RB4=1; //5V to sensor
// TMR 0 configuation
T0CS=0;
PSA=0;
PS2=1; // prescale 1:256
PS1=1;
PS0=1;
TMR0IE=1; // TMR0 Interrupt
TMR0=0;
GIE = 1; //global interrupt
delay(250); //sensor module power up time
}
#define PIC_CLK 20000000 //change this to 3.6864, 4, 16 or 20MHz
//============================================================================
// Include
//============================================================================
#include <pic.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//============================================================================
// Configuration
//============================================================================
__CONFIG ( 0x3F32 );
//============================================================================
// Function Prototype
//============================================================================
void ultrasonic(void); // Find range based on input
void init(void);
void rangecal(unsigned int distance);
void ir_sensor(void);
unsigned int Adc_Read(char channel);
void ad_function(void);
void delay(unsigned int count);
//============================================================================
// global variable
unsigned int To = 0;
unsigned int sysclock;
//============================================================================
// interrupt prototype
//============================================================================================================
void interrupt isr(void)
{
if(TMR0IF) // TMR0 is overflow
{
TMR0IF = 0; // clear flag bit
To += 0x100; // count number of TMR0 overflow ( make it to 16bit TMR)
sysclock++;
}
}
void delay(unsigned int count)
{
unsigned int delay = sysclock;
while((sysclock - delay) < count){
;
}
}
//============================================================================
// Main Function
//============================================================================
void main(void)
{
init();
ultrasonic();
}
// Initailization
// Description : Initialize the microcontroller
//============================================================================================================
void init()
{
TRISB = 0b00000110; //set RB2 pin as input, other as output
TRISC = 0b00000000; //set PORTA as output
TRISD = 0b00000000;
ADCON1 = 0x80; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
RD1=1;
RB4=1; //5V to sensor
// TMR 0 configuation
T0CS=0;
PSA=0;
PS2=1; // prescale 1:256
PS1=1;
PS0=1;
TMR0IE=1; // TMR0 Interrupt
TMR0=0;
GIE = 1; //global interrupt
delay(250); //sensor module power up time
}
//================================================================================
// FUNCTIONS
//================================================================================
//**************************************************
//Calculate the range from the sensor depending on PWM signal input
void ultrasonic(void)
{
unsigned int distance, value, TH;
while(1)
{
if(RB2 == 0) //if RB2 is high
{
TMR0 = 0; // clear all counter involved, start new count for period of RB2 high
To = 0;
}
else
{
TH = TMR0 + To; // RB2 is 0 mean is falling from 1 save TH, RB2 high period
value = TH; //value of tmr0+to
distance = (unsigned int)(value * 1.75616); // calculate inch value per inch = 147us with 20Mhz internal clock.
rangecal(distance);
}
ad_function();
}
}
void rangecal(unsigned int distance)
{
if(distance>300){
PORTD=0B00000010;
}
else if(distance>270){ // 150cm
PORTD=0B00010010;
}
else if(distance>215){ // 120cm
PORTD=0B00100010;
}
else if(distance>140){ //80cm
PORTD=0B01000010;
}
else if(distance>90){ //0 to 50cm
PORTD=0B10000010;
}
}
/*--- A/D function ---*/
void ad_function(void)
{
unsigned int ADC_result;
ADC_result = Adc_Read(2);
if(ADC_result <= 205){
PORTC= 0b00010000;
}
else if((ADC_result >= 206) && (ADC_result <= 410)){
PORTC= 0b00100000;
}
else{
PORTC = 0;
}
}
/*--- Read A/D conversion ---*/
unsigned int Adc_Read(unsigned char channel)
{
unsigned int result = 0U;
unsigned char acquisition_time = 3U;
ADCON1 = 0x80U; /* 10 bit Right justified result, Vdd as ref */
ADCON0 = 0x81U; /* Conversion clock Fosc/32 */
ADCON0 |= (unsigned char)(channel << 2U); /* Select channel */
while(acquisition_time--){ /* Sample channel */
;
}
ADGO = 1U; /* Start conversion */
while(ADGO){ /* Wait for conversion end */
;
}
result = ADRESH;
result <<= 8U;
result |= ADRESL;
return result;
}
/*--- End of File ---*/
void main(void)
{
init();
ultrasonic();
}
// Initailization
// Description : Initialize the microcontroller
//============================================================================================================
void init()
{
TRISB = 0b00000110; //set RB2 pin as input, other as output
TRISC = 0b00000000; //set PORTA as output
TRISD = 0b00000000;
ADCON1 = 0x80; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
RD1=1;
RB4=1; //5V to sensor
// TMR 0 configuation
T0CS=0;
PSA=0;
PS2=1; // prescale 1:256
PS1=1;
PS0=1;
TMR0IE=1; // TMR0 Interrupt
TMR0=0;
GIE = 1; //global interrupt
delay(250); //sensor module power up time
THEN IT JUMPS TO:
void delay(unsigned int count)
{
unsigned int delay = sysclock;
while((sysclock - delay) < count){
AND THEN STOPS RIGHT THERE !
{
unsigned int distance, value, TH;
while(1)
{
if(RB2 == 0) //if RB2 is high
{
TMR0 = 0; // clear all counter involved, start new count for period of RB2 high
To = 0;
}
else
{
TH = TMR0 + To; // RB2 is 0 mean is falling from 1 save TH, RB2 high period
value = TH; //value of tmr0+to
distance = (unsigned int)(value * 1.75616); // calculate inch value per inch = 147us with 20Mhz internal clock.
rangecal(distance);
}
ad_function();
}
}
/*--- Initialise Timer 1 ---*/
static void init_timer1(void)
{
T1CON = 0x30; /* Prescale 1:8 */
TMR1IF = 0;
TMR1IE = 0;
TMR1ON = 1;
}
void interrupt isr(void)
{
if(TMR0IF) // TMR0 is overflow
{
TMR0IF = 0; // clear flag bit
sysclock++;
TMR0 = 0xeb; /* Pre-load for 1mS interrupt */
}
}
#define PIC_CLK 20000000 //change this to 3.6864, 4, 16 or 20MHz
//============================================================================
// Include
//============================================================================
#include <pic.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//============================================================================
// Configuration
//============================================================================
__CONFIG ( 0x3F32 );
//============================================================================
// Function Prototype
//============================================================================
void ultrasonic(void); // Find range based on input
void init(void);
void rangecal(unsigned int distance);
void ir_sensor(void);
unsigned int Adc_Read(char channel);
void ad_function(void);
void delay(unsigned int count);
//============================================================================
// global variable
unsigned int To = 0;
unsigned int sysclock;
//============================================================================
// interrupt prototype
//============================================================================================================
void interrupt isr(void)
{
if(TMR0IF) // TMR0 is overflow
{
TMR0IF = 0; // clear flag bit
sysclock++;
TMR0 = 0xeb; /* Pre-load for 1mS interrupt */
}
}
/*--- Initialise Timer 1 ---*/
static void init_timer1(void)
{
T1CON = 0x30; /* Prescale 1:8 */
TMR1IF = 0;
TMR1IE = 0;
TMR1ON = 1;
}
void delay(unsigned int count)
{
unsigned int delay = sysclock;
while((sysclock - delay) < count){
;
}
}
//============================================================================
// Main Function
//============================================================================
void main(void)
{
init();
ultrasonic();
}
// Initailization
// Description : Initialize the microcontroller
//============================================================================================================
void init()
{
TRISB = 0b00000110; //set RB2 pin as input, other as output
TRISC = 0b00000000; //set PORTA as output
TRISD = 0b00000000;
ADCON1 = 0x80; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
RD1=1;
RB4=1; //5V to sensor
// TMR 0 configuation
T0CS=0;
PSA=0;
PS2=1; // prescale 1:256
PS1=1;
PS0=1;
TMR0IE=1; // TMR0 Interrupt
TMR0=0;
GIE = 1; //global interrupt
delay(250); //sensor module power up time
}
//================================================================================
// FUNCTIONS
//================================================================================
//**************************************************
//Calculate the range from the sensor depending on PWM signal input
void ultrasonic(void)
{
unsigned int distance, value, TH;
while(1)
{
if(RB2 == 0) //if RB2 is high
{
TMR0 = 0; // clear all counter involved, start new count for period of RB2 high
To = 0;
}
else
{
TH = TMR0 + To; // RB2 is 0 mean is falling from 1 save TH, RB2 high period
value = TH; //value of tmr0+to
distance = (unsigned int)(value * 1.75616); // calculate inch value per inch = 147us with 20Mhz internal clock.
rangecal(distance);
}
ad_function();
}
}
void rangecal(unsigned int distance)
{
if(distance>300){
PORTD=0B00000010;
}
else if(distance>270){ // 150cm
PORTD=0B00010010;
}
else if(distance>215){ // 120cm
PORTD=0B00100010;
}
else if(distance>140){ //80cm
PORTD=0B01000010;
}
else if(distance>90){ //0 to 50cm
PORTD=0B10000010;
}
}
/*--- A/D function ---*/
void ad_function(void)
{
unsigned int ADC_result;
ADC_result = Adc_Read(2);
if(ADC_result <= 205){
PORTC= 0b00010000;
}
else if((ADC_result >= 206) && (ADC_result <= 410)){
PORTC= 0b00100000;
}
else{
PORTC = 0;
}
}
/*--- Read A/D conversion ---*/
unsigned int Adc_Read(unsigned char channel)
{
unsigned int result = 0U;
unsigned char acquisition_time = 3U;
ADCON1 = 0x80U; /* 10 bit Right justified result, Vdd as ref */
ADCON0 = 0x81U; /* Conversion clock Fosc/32 */
ADCON0 |= (unsigned char)(channel << 2U); /* Select channel */
while(acquisition_time--){ /* Sample channel */
;
}
ADGO = 1U; /* Start conversion */
while(ADGO){ /* Wait for conversion end */
;
}
result = ADRESH;
result <<= 8U;
result |= ADRESL;
return result;
}
/*--- End of File ---*/
#define PIC_CLK 20000000 //change this to 3.6864, 4, 16 or 20MHz
//============================================================================
// Include
//============================================================================
#include <pic.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//============================================================================
// Configuration
//============================================================================
__CONFIG ( 0x3F32 );
//============================================================================
// Function Prototype
//============================================================================
void ultrasonic(void); // Find range based on input
void init(void);
void rangecal(unsigned int distance);
void ir_sensor(void);
unsigned int Adc_Read(char channel);
void ad_function(void);
void delay(unsigned int count);
void init_timer1(void);
//============================================================================
// global variable
unsigned int To = 0;
unsigned int sysclock;
//============================================================================
// interrupt prototype
//============================================================================================================
/*--- Initialise Timer 1 ---*/
void init_timer1(void)
{
T1CON = 0x30; /* Prescale 1:8 */
TMR1IF = 0;
TMR1IE = 0;
TMR1ON = 1;
}
void interrupt isr(void)
{
if(TMR0IF) // TMR0 is overflow
{
TMR0IF = 0; // clear flag bit
sysclock++;
TMR0 = 0xeb; /* Pre-load for 1mS interrupt */
}
}
void delay(unsigned int count)
{
unsigned int delay = sysclock;
while((sysclock - delay) < count){
;
}
}
//============================================================================
// Main Function
//============================================================================
void main(void)
{
init_timer1();
init();
ultrasonic();
}
// Initailization
// Description : Initialize the microcontroller
//============================================================================================================
void init()
{
TRISB = 0b00000110; //set RB2 pin as input, other as output
TRISC = 0b00000000; //set PORTA as output
TRISD = 0b00000000;
ADCON1 = 0x80; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
RD1=1;
RB4=1; //5V to sensor
// TMR 0 configuation
T0CS=0;
PSA=0;
PS2=1; // prescale 1:256
PS1=1;
PS0=1;
TMR0IE=1; // TMR0 Interrupt
TMR0=0;
GIE = 1; //global interrupt
delay(250); //sensor module power up time
}
//================================================================================
// FUNCTIONS
//================================================================================
//**************************************************
//Calculate the range from the sensor depending on PWM signal input
void ultrasonic(void)
{
unsigned int distance, value;
while(1)
{
while(RB2 == 0){
;
}
TMR1L = 0;
TMR1H = 0;
TMR1ON = 1;
while(RB2 == 1){
;
}
TMR1ON = 0;
value = TMR1H;
value <<= 8U;
value |= TMR1L;
distance = (unsigned int)(value * 1.75616); // calculate inch value per inch = 147us with 20Mhz internal clock.
rangecal(distance);
ad_function();
}
}
void rangecal(unsigned int distance)
{
if(distance>300){
PORTD=0B00000010;
}
else if(distance>270){ // 150cm
PORTD=0B00010010;
}
else if(distance>215){ // 120cm
PORTD=0B00100010;
}
else if(distance>140){ //80cm
PORTD=0B01000010;
}
else if(distance>90){ //0 to 50cm
PORTD=0B10000010;
}
}
/*--- A/D function ---*/
void ad_function(void)
{
unsigned int ADC_result;
ADC_result = Adc_Read(2);
if(ADC_result <= 205){
PORTC= 0b00010000;
}
else if((ADC_result >= 206) && (ADC_result <= 410)){
PORTC= 0b00100000;
}
else{
PORTC = 0;
}
}
/*--- Read A/D conversion ---*/
unsigned int Adc_Read(unsigned char channel)
{
unsigned int result = 0U;
unsigned char acquisition_time = 3U;
ADCON1 = 0x80U; /* 10 bit Right justified result, Vdd as ref */
ADCON0 = 0x81U; /* Conversion clock Fosc/32 */
ADCON0 |= (unsigned char)(channel << 2U); /* Select channel */
while(acquisition_time--){ /* Sample channel */
;
}
ADGO = 1U; /* Start conversion */
while(ADGO){ /* Wait for conversion end */
;
}
result = ADRESH;
result <<= 8U;
result |= ADRESL;
return result;
}
/*--- End of File ---*/
void ultrasonic(void)
{
while(1)
{
if(RB2==0) //if RB2 is high
{
TMR0=0; // clear all counter involved, start new count for period of RB2 high
To=0;
}
else
{
TH=TMR0+To; // RB2 is 0 mean is falling from 1 save TH, RB2 high period
value=TH; //value of tmr0+to
distance=value*1.75616; // calculate inch value per inch = 147us with 20Mhz internal clock.
rangecal();
}
}
}
void rangecal(void)
{
if(distance>300)
{
PORTD=0B00000010;
}
else if(distance>270) // 150cm
{
PORTD=0B00010010;
}
else if(distance>215) // 120cm
{
PORTD=0B00100010;
}
else if(distance>140) //80cm
{
PORTD=0B01000010;
}
else if(distance>90) //0 to 50cm
{
PORTD=0B10000010;
}
}
void ultrasonic(void)
{
unsigned int distance, value;
while(1)
{
while(RB2 == 0){
;
}
TMR1L = 0;
TMR1H = 0;
TMR1ON = 1;
while(RB2 == 1){
;
}
TMR1ON = 0;
value = TMR1H;
value <<= 8U;
value |= TMR1L;
distance = (unsigned int)(value * 1.75616); // calculate inch value per inch = 147us with 20Mhz internal clock.
rangecal(distance);
ad_function();
}
}
void rangecal(unsigned int distance)
{
if(distance>300){
PORTD=0B00000010;
}
else if(distance>270){ // 150cm
PORTD=0B00010010;
}
else if(distance>215){ // 120cm
PORTD=0B00100010;
}
else if(distance>140){ //80cm
PORTD=0B01000010;
}
else if(distance>90){ //0 to 50cm
PORTD=0B10000010;
}
}
while(RB2 == 0){
;
}
TMR1L = 0;
TMR1H = 0;
TMR1ON = 1;
while(RB2 == 1){
;
}
TMR1ON = 0;
value = TMR1H;
value <<= 8U;
value |= TMR1L;
while(RB2 == 1){
;
}
TMR1L = 0;
TMR1H = 0;
TMR1ON = 1;
while(RB2 == 0){
;
}
TMR1ON = 0;
value = TMR1H;
value <<= 8U;
value |= TMR1L;
while(RB2 == 0){ /* Wait for RB2 to go high */
;
}
TMR1L = 0;
TMR1H = 0;
TMR1ON = 1; /* Zero the counter, start counting */
while(RB2 == 1){ /* Wait for RB2 to go low */
;
}
TMR1ON = 0; /* Stop the counter, save the counter value */
value = TMR1H;
value <<= 8U;
value |= TMR1L;
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?