RobotHeart
Junior Member level 3
Hello every1....this is my 1st post in this forum, and i hope i can get some help.
Im using PIC16F877A MCU, and I want to change the format of a code which do the following:
1/ convert the analog voltage received in PortA.2 (pin 4) to digital.
2/ if the analog voltage is less than 2V and more than 1V...set PortC.4 (pin 23).
3/ If analog voltage is less than 1V...set PortC.5 (pin 24).
thats it !!!!
the code i have works in MikroC and its working very well...i have already tested it in my hradware.....but i want to make it work in MPLAB (HI-TECH)...the code i have is :
my main code actually is in MPLAB format....thats why i want to change it.
my main code is (no need for changes...only to help understanding):
This code works on MPLAB....so i want to add the first code to this one.
please help...im running out of time here.
thank you
Im using PIC16F877A MCU, and I want to change the format of a code which do the following:
1/ convert the analog voltage received in PortA.2 (pin 4) to digital.
2/ if the analog voltage is less than 2V and more than 1V...set PortC.4 (pin 23).
3/ If analog voltage is less than 1V...set PortC.5 (pin 24).
thats it !!!!
the code i have works in MikroC and its working very well...i have already tested it in my hradware.....but i want to make it work in MPLAB (HI-TECH)...the code i have is :
Code:
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; }
}
my main code actually is in MPLAB format....thats why i want to change it.
my main code is (no need for changes...only to help understanding):
Code:
#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;
}
}
This code works on MPLAB....so i want to add the first code to this one.
please help...im running out of time here.
thank you