I planning to use ultrasonic sensor which had PWM output which can direct use/read in Pic16f877A. But in programming wise, I facing problem in using timer. Anyone can help?
I referred sample code in Cytron DIY PR23 but its ain't help me much.
Sample code I modified:
- CODE: SELECT_ALL_CODE
//============================================================================
// Include
//============================================================================
#include <pic.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "delay.h"
#include "delay.c"
//============================================================================
// Configuration
//============================================================================
__CONFIG ( 0x3F32 );
//============================================================================
// Define
//============================================================================
//============================================================================
// Function Prototype
//============================================================================
void rangecalc(void); // Find range based on input
void init(void);
//============================================================================
// global variable
//============================================================================
// Sensor declarations
unsigned int To=0,TH=0;
unsigned int value; // Range to be used for calculation
//============================================================================
// Main Function
//============================================================================
void main(void)
{
DelayMs(800);
init();
rangecalc();
}
// Initailization
// Description : Initialize the microcontroller
//============================================================================================================
void init()
{
// Tris configuration (input or output)
TRISA = 0b00000011; //set RA0 and RA1 pin as input,other as output
// TMR 0 configuation
T0CS = 0;
PSA = 0;
PS2 = 1; // prescale 1:32
PS1 = 1; //
PS0 = 1; //
TMR0IE = 1; // TMR0 Interrupt
TMR0 = 0;
}
//================================================================================
// FUNCTIONS
//================================================================================
//**************************************************
// rangecalc() - Calculate the range from the sensor depending on type of range
void rangecalc(void)
{
if(RA1 == 0)
{
TMR0 = 0; // clear all counter involved, start new count for period of RA1 high
To = 0;
}
else
{
TH = TMR0 + To; // RA1 is 0 mean is falling form 1, save TH, RA1 high period
value = TH;
}
}
Any comment?