unsigned int t;
void main() {
ADCON1 = 0x80; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
TRISC = 0; // PORTD is output
while (1)
t = Adc_Read(2); // get ADC value from 2nd channel
if(t >= 0 & t <= 410){
PORTC= 0b00010000;
}
else if (t >= 411 & t <= 615 ){
PORTC= 0b00100000;
}
else PORTC = 0; }
}
#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>
#include "delay.h"
#include "delay.c"
//============================================================================
// Configuration
//============================================================================
__CONFIG ( 0x3F32 );
//============================================================================
// Function Prototype
//============================================================================
void ultrasonic(void); // Find range based on input
void init(void);
void rangecal(void);
//============================================================================
// global variable
unsigned int To=0;
unsigned int TH=0;
unsigned int value;
unsigned int distance;
unsigned int data=0;
//============================================================================
// interrupt prototype
//============================================================================================================
static 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)
}
}
//============================================================================
// Main Function
//============================================================================
void main(void)
{
init();
ultrasonic();
}
// Initailization
// Description : Initialize the microcontroller
//============================================================================================================
void init()
{
// Tris configuration (input or output)
TRISA = 0b00010001; //set PORTA as output
TRISB = 0b00000110; //set RB2 pin as input, other as output
TRISC = 0b00000000; //set PORTA as output
TRISD = 0b00000000;
RD1=1;
RB4=1; //5V to sensor
DelayMs(250); //sensor module power up time
// 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
}
//================================================================================
// FUNCTIONS
//================================================================================
//**************************************************
//Calculate the range from the sensor depending on PWM signal input
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;
}
}
unsigned int t;
void main()
{
ADCON1 = 0x80; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
TRISC = 0; // PORTD is output
while (1)
t = Adc_Read(2); // get ADC value from 2nd channel /* Infinite loop? */
/* The program never gets this far ??? */
if(t >= 0 & t <= 410){ /* This expression won't work! */
PORTC= 0b00010000;
}
else if(t >= 411 & t <= 615 ){ /* This expression won't work! */
PORTC= 0b00100000;
}
else PORTC = 0;
}
} /* Extra bracket? */
unsigned int t;
void main() {
ADCON1 = 0x80; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
TRISC = 0; // PORTD is output
while (1) {
t = Adc_Read(2); // get ADC value from 2nd channel
if(t >= 0 && t <= 410){
PORTC= 0b00010000;
}
else if (t >= 411 && t <= 615 ){
PORTC= 0b00100000;
}
else PORTC = 0; }
}
unsigned int t;
void main()
{
ADCON1 = 0x80; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
TRISC = 0; // PORTD is output
while(1)
{
t = Adc_Read(2); // get ADC value from 2nd channel
if((t >= 0) && (t <= 410)){
PORTC = 0b00010000;
}
else if((t >= 411) && (t <= 615)){
PORTC = 0b00100000;
}
else{
PORTC = 0;
}
}
}
#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>
#include "delay.h"
#include "delay.c"
//============================================================================
// Configuration
//============================================================================
__CONFIG ( 0x3F32 );
//============================================================================
// Function Prototype
//============================================================================
void ultrasonic(void); // Find range based on input
void init(void);
void rangecal(void);
void IR_detection (void);
//============================================================================
// global variable
unsigned int To=0;
unsigned int TH=0;
unsigned int value;
unsigned int distance;
unsigned int data=0;
unsigned int t;
//============================================================================
// interrupt prototype
//============================================================================================================
static 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)
}
}
//============================================================================
// Main Function
//============================================================================
void main(void)
{
init();
ultrasonic();
}
// Initailization
// Description : Initialize the microcontroller
//============================================================================================================
void init()
{
// Tris configuration (input or output)
TRISA = 0b00010001; //set PORTA as output
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
TRISC = 0; // PORTD is output
RD1=1;
RB4=1; //5V to sensor
DelayMs(250); //sensor module power up time
// 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
}
//================================================================================
// FUNCTIONS
//================================================================================
//**************************************************
//Calculate the range from the sensor depending on PWM signal input
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 IR_detection (void) {
while(1)
{
t = Adc_Read(2); // get ADC value from 2nd channel
if ((t >= 0) && (t <= 410)) {
PORTC = 0b00010000;
}
else if((t >= 411) && (t <= 615)){
PORTC = 0b00100000;
}
else{
PORTC = 0;
}
}
}
#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>
#include "delay.h"
#include "delay.c"
#define START_CONVERSION ADGO = 1
#define ADC_BUSY ADGO
//============================================================================
// Configuration
//============================================================================
__CONFIG ( 0x3F32 );
//============================================================================
// Function Prototype
//============================================================================
void ultrasonic(void); // Find range based on input
void init(void);
void rangecal(void);
void ir_sensor(void);
void Init_ADC(void);
unsigned int Start_ADC_conversion(void);
//============================================================================
// global variable
unsigned int To=0;
unsigned int TH=0;
unsigned int value;
unsigned int distance;
unsigned int data=0;
unsigned int ADC_result;
//============================================================================
// interrupt prototype
//============================================================================================================
static 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)
}
}
//============================================================================
// Main Function
//============================================================================
void main(void)
{
init();
ultrasonic();
}
// Initailization
// Description : Initialize the microcontroller
//============================================================================================================
void init()
{
// Tris configuration (input or output)
TRISB = 0b00000110; //set RB2 pin as input, other as output
TRISC = 0b00000000; //set PORTA as output
TRISD = 0b00000000;
DelayMs(250); //sensor module power up time
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
}
//================================================================================
// FUNCTIONS
//================================================================================
//**************************************************
//Calculate the range from the sensor depending on PWM signal input
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 Init_ADC(void)
{
ADCON0 = 0X81;
ADCON1 = 0X80; //RA0 analog,RA3 analog reference, rest digital IO's
}
//*******************************************************************
// This function converts the ADC Channel 0(RA0)
// to its equivalent digital output
//*******************************************************************
unsigned int Start_ADC_conversion(void)
{
unsigned int ADC_result;
START_CONVERSION;
while(ADC_BUSY){
ADC_result = ADRESH;
ADC_result = (ADC_result << ADRESL);
while (1) {
if(ADC_result >= 0 & ADC_result <= 205){
PORTC= 0b00010000;
}
else if (ADC_result >= 206 & ADC_result <= 410 ){
PORTC= 0b00100000;
}
else PORTC = 0;
}
return(ADC_result);
}
}
unsigned int Adc_Read(char channel);
/*--- 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 */
;
}
GODONE = 1U; /* Start conversion */
while(GODONE){ /* Wait for conversion end */
;
}
result = ADRESH;
result <<= 8U;
result |= ADRESL;
return result;
}
unsigned int Start_ADC_conversion(void)
{
unsigned int ADC_result;
START_CONVERSION;
while(ADC_BUSY){
/* You are reading the result while the conversion is in progress! */
ADC_result = ADRESH;
ADC_result = (ADC_result << ADRESL);
/* This starts an infinate loop that never ends */
while (1) {
if(ADC_result >= 0 & ADC_result <= 205){
PORTC= 0b00010000;
}
else if (ADC_result >= 206 & ADC_result <= 410 ){
PORTC= 0b00100000;
}
else PORTC = 0;
}
/* Code never gets here */
return(ADC_result);
}
}
#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;
delay(250); //sensor module power up time
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
}
//================================================================================
// 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 */
;
}
GODONE = 1U; /* Start conversion */
while(GODONE){ /* 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>
#include "delay.h"
#include "delay.c"
//============================================================================
// Configuration
//============================================================================
__CONFIG ( 0x3F32 );
//============================================================================
// Function Prototype
//============================================================================
void ultrasonic(void); // Find range based on input
void init(void);
void rangecal(void);
void IR_detection (void);
unsigned int Adc_Read(char channel);
unsigned int Adc_Read(unsigned char channel);
//============================================================================
// global variable
unsigned int To=0;
unsigned int TH=0;
unsigned int value;
unsigned int distance;
unsigned int data=0;
unsigned int t;
//============================================================================
// interrupt prototype
//============================================================================================================
static 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)
}
}
//============================================================================
// Main Function
//============================================================================
void main(void)
{
init();
ultrasonic();
}
// Initailization
// Description : Initialize the microcontroller
//============================================================================================================
void init()
{
// Tris configuration (input or output)
TRISA = 0b00010001; //set PORTA as output
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
TRISC = 0; // PORTD is output
RD1=1;
RB4=1; //5V to sensor
DelayMs(250); //sensor module power up time
// 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
}
//================================================================================
// FUNCTIONS
//================================================================================
//**************************************************
//Calculate the range from the sensor depending on PWM signal input
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;
}
}
unsigned int Adc_Read(char channel);
/*--- 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 */
;
}
GODONE = 1U; /* Start conversion */
while(GODONE){ /* Wait for conversion end */
;
}
result = ADRESH;
result <<= 8U;
result |= ADRESL;
while (1) {
if(result >= 0 & result <= 205){
PORTC= 0b00010000;
}
else if (result >= 206 & result <= 410 ){
PORTC= 0b00100000;
}
else PORTC = 0;
}
return result;
}
#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>
#include "delay.h"
#include "delay.c"
//============================================================================
// Configuration
//============================================================================
__CONFIG ( 0x3F32 );
//============================================================================
// Function Prototype
//============================================================================
void ultrasonic(void); // Find range based on input
void init(void);
void rangecal(void);
void IR_detection (void);
unsigned int Adc_Read(char channel);
unsigned int Adc_Read(unsigned char channel);
//============================================================================
// global variable
unsigned int To=0;
unsigned int TH=0;
unsigned int value;
unsigned int distance;
unsigned int data=0;
unsigned int t;
//============================================================================
// interrupt prototype
//============================================================================================================
static 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)
}
}
//============================================================================
// Main Function
//============================================================================
void main(void)
{
init();
ultrasonic();
}
// Initailization
// Description : Initialize the microcontroller
//============================================================================================================
void init()
{
// Tris configuration (input or output)
TRISA = 0b00010001; //set PORTA as output
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
TRISC = 0; // PORTD is output
RD1=1;
RB4=1; //5V to sensor
DelayMs(250); //sensor module power up time
// 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
}
//================================================================================
// FUNCTIONS
//================================================================================
//**************************************************
//Calculate the range from the sensor depending on PWM signal input
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;
}
}
unsigned int Adc_Read(char channel);
/*--- 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;
while(1)
{
t = Adc_Read(2); // get ADC value from 2nd channel
if((t >= 0) && (t <= 410)){
PORTC = 0b00010000;
}
else if((t >= 411) && (t <= 615)){
PORTC = 0b00100000;
}
else{
PORTC = 0;
}
}
return result;
}
#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;
delay(250); //sensor module power up time
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
}
//================================================================================
// 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>
#include "delay.h"
#include "delay.c"
//============================================================================
// Configuration
//============================================================================
__CONFIG ( 0x3F32 );
//============================================================================
// Function Prototype
//============================================================================
void ultrasonic(void); // Find range based on input
void init(void);
void rangecal(void);
void IR_detection (void);
unsigned int Adc_Read(char channel);
unsigned int Adc_Read(unsigned char channel);
//============================================================================
// global variable
unsigned int To=0;
unsigned int TH=0;
unsigned int value;
unsigned int distance;
unsigned int data=0;
unsigned int t;
unsigned int ADC_result;
unsigned int result;
//============================================================================
// interrupt prototype
//============================================================================================================
static 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)
}
}
//============================================================================
// Main Function
//============================================================================
void main(void)
{
init();
ultrasonic();
}
// Initailization
// Description : Initialize the microcontroller
//============================================================================================================
void init()
{
// Tris configuration (input or output)
TRISA = 0b00010001; //set PORTA as output
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
TRISC = 0; // PORTD is output
RD1=1;
RB4=1; //5V to sensor
DelayMs(250); //sensor module power up time
// 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
}
//================================================================================
// FUNCTIONS
//================================================================================
//**************************************************
//Calculate the range from the sensor depending on PWM signal input
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;
}
}
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;
}
void ad_function(void)
{
unsigned int ADC_result;
if(result <= 205){
PORTC= 0b00010000;
}
else if((result >= 206) && (result <= 410)){
PORTC= 0b00100000;
}
else{
PORTC = 0;
}
}
.
.
.
/*--- 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;
}
}
.
.
.
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?