Microcontroller: PIC16F877A
IDE: MikroC Pro
Oscillator: XT 20Mhz
---------------------------
Hi everyone, I am not new to electronic forums but I just registered with recently with this forum hoping that there will be some who will enlighten me regarding my problems which cannot be solved on other forums.
First, to explain my project, I am using a gyroscope and an accelerometer as my inputs and two motors as my outputs. Initially, I tested putting a varying DC input with my PIC16F877A in Proteus. I have generated the program below, the only problem I am having right now is, I want to display the value my PIC calculated from my gyroscope on a 16x2 LCD which I cannot make. Here is the code:
Code:
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
signed int gz_adc;
unsigned int gz_deg;
unsigned char value;
unsigned short motor1;
void main()
{
Lcd_Init(); // LCD display initialization
Lcd_Cmd(_LCD_CURSOR_OFF); // LCD command (cursor off)
Lcd_Cmd(_LCD_CLEAR);
ADCON1 = 0x82;
TRISA = 0x03; //RA0 & RA1 as inputs
TRISC = 0x00; //Setting Port C as outputs for PWM
PORTC = 0x00;
PWM1_Init(5000);
PWM1_Start();
while (1)
{
gz_adc=ADC_Read(0); //Read gyroscope
motor1=(gz_adc/4); //setting a value in motor1
gz_deg=(((gz_adc*5/1023)-2.5)/.005); //convert value from gyroscope in deg/s
LCD_Chr(1,1,48+gz_deg); //put the deg/s value as display on the LCD [I][B]PROBLEM IS HERE[/B][/I]
PWM1_Set_Duty(motor1); //Just runs the motor
delay_ms(100);
}
}
On Proteus, the value does not seems to display even just a digit of it. My suspicion is that, the value is not in a proper form in order to be displayed on the LCD. The reason why I am asking for help.
I will give an example of a gyroscope reading in deg/s from starlino webpage:
gz_deg = (323 * 3.3V / 1023 – 1.23V) / ( 0.002V/deg/s) =~ -94 deg/s
For kind people out there, I need a help on how to display the signed value.
Thanks!
PS
If I missed some relevant info that I should included with my post, please tell it.