PIC C Compiling Errors with PIC16F877A - c

I'm trying to compile the following code
#include <16f877a.h> //PIC SELECTION
#fuses hs, NOWDT, BROWNOUT,noPUT, NOLVP //FUSES CONFIGURATIONS
#use delay (clock=8000000) //4MHZ OSC
#INCLUDE <lcd.c> //INCLUDE LCD.C
#define use_portb_kbd TRUE //USE PORT B FOR KEYPAD
#INCLUDE <kbd.c> //include Keypad
unsigned char kbd_read()
{
unsigned char C;
C=kbd_getc();
while(C==’\0′) {
C=kbd_getc();
}
}
void main()
{
char c[4];
kbd_init(); //KEYPAD INIT.
lcd_init();
lcd_putc(” WELCOME To \nKahrabje Coures”);
delay_ms(3000);
while(1)
{
start:
lcd_putc(“\f type password\n”);
c[0]=kbd_read();lcd_putc(‘*’);//if(c[0]!=’1′);lcd_putc(“\f faild”);
delay_ms(1000);goto start;
c[1]=kbd_read();lcd_putc(‘*’);
c[2]=kbd_read();lcd_putc(‘*’);
c[3]=kbd_read();lcd_putc(‘*’);
if(c[0]==’1’&& c[1]==’2′ && c[2]==’3′ && c[3]==’4′){
printf(lcd_putc,”\fwelcome”);
}
else {
printf(lcd_putc,”\ffaild”);
}
delay_ms(1000);
}
}
First I had Error 18(file can not be found), after a few attempts (using project wizard, making sure of file name.. etc) I ended up with Error 23 (Can not change device type this far into the code)
Any idea why I'm having these errors?
Thanks for your time.

Related

error function declared but never defined

I have added a library to my c project in codeVision AVR. when I want to use it's functions receive this error:
function 'function name' is declared but never defined.
here is my code:
#include "pid.h"
#include <mega32.h>
PidType _pid;
void main(void)
{
//some uC hardware initializing codes which are removed here to simplify code
PID_Compute(&_pid);
while (1)
{
// Place your code here
}
}
in pid.h:
.
.
bool PID_Compute(PidType* pid);
.
.
and pid.c:
#include "pid.h"
.
.
bool PID_Compute(PidType* pid) {
if (!pid->inAuto) {
return false;
}
FloatType input = pid->myInput;
FloatType error = pid->mySetpoint - input;
pid->ITerm += (pid->ki * error);
if (pid->ITerm > pid->outMax)
pid->ITerm = pid->outMax;
else if (pid->ITerm < pid->outMin)
pid->ITerm = pid->outMin;
FloatType dInput = (input - pid->lastInput);
FloatType output = pid->kp * error + pid->ITerm - pid->kd * dInput;
if (output > pid->outMax)
output = pid->outMax;
else if (output < pid->outMin)
output = pid->outMin;
pid->myOutput = output;
pid->lastInput = input;
return true;
}
the ERROR:
function 'PID_Compute' declared, but never defined.
Where is the problem?
EDIT:
to add the library to my project I placed the .c and .h library files in the same folder that my main project file is:
and then #include "pid.h" in my main file:
#include "pid.h"
#include <mega32.h>
// Declare your global variables here
PidType _pid;
void main(void)
{
.
.
my error and warnings:
EDIT2:
I simplified the code and now can show you the entire code:
main code:
#include "pid.h"
PidType _pid;
void main(void)
{
PID_Compute(&_pid);
while (1)
{
}
}
pid.h:
#ifndef PID_H
#define PID_H
#include <stdbool.h>
typedef struct {
int i;
} PidType;
bool PID_Compute(PidType* pid);
#endif
pid.c:
#include "pid.h"
bool PID_Compute(PidType* pid) {
pid->i = 2;
return true;
}
thank you every body.
As you said, the pid.c was not added to the project.
for those who may face the same problem:
in codeVision AVR we have to add .c file to project from project->configure->files->input files->add
I addec .c file to project and the error went away.
From the screenshot with the tree view of your files it is clear that the file "pid.c" is not part of the project.
Move it into your project. Then it should build without that linker error.
This does not mean the location in the file system. I reference the "virtual" view of the IDE on your project.

STMF4 and USB OTG using FATfs

I am using STM32F407 Discovery Board for interfacing USB OTG FS. I am using CubeMx and Keil for development.
First thing first, I have enabled PC0 - USB_Power(for Discovery Board) and the state is RESET for proper USB running.I have enabled PA9 - VBUS as GPIO Input.My System is running at 168MHz.Have used MAX_SS(Max Sector size) - 4096(This option is available in Cube Mx).Enabled USB as Host and used FATFS provided by CubeMX.Enbaled MSC(Mass Storage Class).
CODE:
#include "main.h"
#include "stm32f4xx_hal.h"
#include "fatfs.h"
#include "usb_host.h"
#define GREEN_High HAL_GPIO_WritePin(GREEN_GPIO_Port,GREEN_Pin,GPIO_PIN_SET)
#define GREEN_Low HAL_GPIO_WritePin(GREEN_GPIO_Port,GREEN_Pin,GPIO_PIN_RESET)
#define ORANGE_High HAL_GPIO_WritePin(ORANGE_GPIO_Port,ORANGE_Pin,GPIO_PIN_SET)
#define ORANGE_Low HAL_GPIO_WritePin(ORANGE_GPIO_Port,ORANGE_Pin,GPIO_PIN_RESET)
extern USBH_HandleTypeDef hUsbHostFS;
extern ApplicationTypeDef Appli_state;
FATFS USBDISKFatFs;
FIL MyFile;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
void MX_USB_HOST_Process(void);
void Green_Blink(uint16_t ms);
void Orange_Blink(uint16_t ms);
void USB_Write_Demo(char* fileName);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USB_HOST_Init();
MX_FATFS_Init();
Green_Blink(100);
Orange_Blink(100);
while (1)
{
MX_USB_HOST_Process();
if (Appli_state == APPLICATION_START)
{
USB_Write_Demo("myCSV.csv");
}
*THIS IS THE AREA OF PROBLEM*
// else if (Appli_state == APPLICATION_IDLE)
// {
// GREEN_High;
// ORANGE_High;
// HAL_Delay(100);
// GREEN_Low;
// ORANGE_Low;
// HAL_Delay(100);
// }
}
}
void USB_Write_Demo(char *fileName)
{
FRESULT fres;
uint32_t bytesWritten;
uint8_t w_text[] = {"Hello, I, AM, STM32, Discovery\r\n"};
if (f_mount(&USBDISKFatFs,(TCHAR const*)USBHPath,0) != FR_OK)
{
Orange_Blink(1000);
Error_Handler();
}
else
{
Green_Blink(100);
if (open_append(&MyFile,fileName) != FR_OK)
{
Orange_Blink(100);
Error_Handler();
}
else
{
Green_Blink(100);
fres = f_write(&MyFile,w_text,sizeof(w_text),(void*)bytesWritten);
if (bytesWritten == 0 || fres != FR_OK)
{
Orange_Blink(100);
Error_Handler();
}
else
{
f_close(&MyFile);
Green_Blink(100);
}
}
}
}
void Green_Blink(uint16_t ms)
{
GREEN_High;
HAL_Delay(ms);
GREEN_Low;
HAL_Delay(ms);
}
void Orange_Blink(uint16_t ms)
{
ORANGE_High;
HAL_Delay(ms);
ORANGE_Low;
HAL_Delay(ms);
}
So what is happening here is i am creating a CSV file and with every loop i am appending the new data in it. And i am really succesffull in doing so. I have created a really long(500KB not so long) csv file using this particular code.
But i have found an abnormality here which i am not able to understand.
When i add this part to the code, there is no file created and every iteration the control reaches this function.
else if (Appli_state == APPLICATION_IDLE)
{
GREEN_High;
ORANGE_High;
HAL_Delay(100);
GREEN_Low;
ORANGE_Low;
HAL_Delay(100);
}
I am not able to understand how this function is affecting the working code. I am sure that APPLICATION_START and APPLICATION_IDLE are two diffrent things. When i comment this portion of the code, Everything is just fine i can make files as long as my storage doesn't end.
It took me several hours(like 2days) to figure out this is the problem.
I tried to increase Minimum heap size - 0x2000 and Minimum Stack size - 0x4000 (This option is available on linker setting in cubeMx. While generating file the place where you give the project name,location and all there only)
Any suggestions will be helpful as i am out of ideas.
I found a way to deal with this problem without using RTOS. As i have never tried RTOS before It was difficult to complete the project in few days.
The idea is simple. It is we need to wait till MX_USB_HOST_Process() doesn't returns Appli_state as Idle.
I dont take the credit.
You can check this LINK
so i added a new function in usb_host.c which retuns Appli_state
uint8_t IsUSB_Busy(void)
{
return Appli_state;
}
And in main.c i waited till it returns anything but 0. As APPLICATION_IDLE=0
typedef enum {
APPLICATION_IDLE = 0,
APPLICATION_START,
APPLICATION_READY,
APPLICATION_DISCONNECT
}ApplicationTypeDef;
Added this bit of code in main file and everything is working as expected
while (!IsUSB_Busy())
{
MX_USB_HOST_Process();
}
I hope someone finds it helpful.
And Thanks for your help.
You can create two Tasks (FreeRTOS) with CubeMX, separate the USB stuff from the LED stuff. taskLED() and taskUSB()

How to avoid using "extern" to share an instance between many files in C?

I made a timer library in c to start a timer, stop it ...etc. I use this lib for making many timers for different events. For each event I should make a new instance of the timer. I use one with serial communication library and one for the keypad library. Now the point is that there is a function timer_interrupt_handler(New_timer* timer) that must be called for each instance by the microcontroller's timer1 interrupt routine which remains in the main file. For this, timers' instances must be of extern type. Is there a way to avoid using extern in this scenario.
timer_tick.h file
typedef enum state{STOPPED=0, RUNNING, TIMEOUT} Timer_state;
typedef struct {
unsigned char volatile state;
unsigned int volatile ticks;
unsigned int volatile timeout_ticks;
}New_timer;
timer_tick.c file
#include "timer_tick.h"
void start_a_timer(New_timer* timer)
{
timer->state = RUNNING;
timer->ticks = 0;
}
void stop_a_timer(New_timer* timer)
{
timer->state = STOPPED;
}
void timer_interrupt_handler(New_timer* timer)
{
if(timer->state==RUNNING)
{
if(timer->ticks < timer->timeout_ticks)
timer->ticks++;
else
timer->state = TIMEOUT;
}
}
main.c
// timer1 interrupt routine
void timerIsr()
{
timer_interrupt_handler(&timer1);
timer_interrupt_handler(&timer2);
timer_interrupt_handler(&timer3);
timer_interrupt_handler(&timer4);
timer_interrupt_handler(&timer5);
}
Why don't you register the instances in the timer module using a linked list?
At client code
static New_timer a_timer;
void foo () {
timer_register (&a_timer);
};
At timer.c:
void timer_register (New_timer * timer) {
timer_add_to_a_linked_list (timer);
}
void timer_interrupt_handler () {
for_each_timer_in_list_do_something ();
}
You can avoid the use of extern in many cases by the use of a getter function.
In client.h:
#include "timer_tick.h"
New_timer* client_get_timer(void);
In client.c:
#include "client.h"
New_timer local_timer;
New_timer* client_get_timer()
{
return &local_timer;
}
In main.c:
#include "client.h"
void timerIsr()
{
timer_interrupt_handler(client_get_timer());
}

The Kernel starts behaving abnormally when the kernel code gets a little bigger

I am developing an OS kernel. I am facing a problem that till a particular size of the kernel - 8KB it runs perfectly but as it gets just a little over 8KB, it starts behaving abnormally. The clear screen function doesn't work, the scrolling function doesn't work etc.
I am using the bochs emulator with a 1.44MB floppy configuration.
My code working correctly is -
#include "functions.h"
#include "stdint.h"
#include "stddef.h"
#include "../drivers/colors.h"
void delay();
char getScancode();
void main()
{
/*Declarations*/
char* str = "Welcome to MyOS v0.2.4 By Anish Sharma 2017";
char* status = "Welcome Anish Sharma";
uint8_t i = 0;
for(i=0;i<80;i++)
putChar(' ',i,24,STATUS_COLOR);
write_string_line(STATUS_COLOR,status,24);
clrscr();
print("The KERNEL has been loaded successfully at 0x1000 (memory address)");
print(str);
print(">>>");
update_cursor(3,2);
for(i=0;i<80;i++)
{
putChar(0xdb,i,3,i);
}
for(i=0;i<80;i++)
{
putChar(0xdb,i,4,i);
}
for(i=0;i<80;i++)
{
putChar(0xdb,i,5,i);
}
for(i=0;i<80;i++)
{
putChar(0xdb,i,6,i);
}
for(i=0;i<80;i++)
{
putChar(0xdb,i,3,i);
}
}
The output is -
Adding just another -
for(i=0;i<80;i++)
{
putChar(0xdb,i,3,i);
}
The output is -
Can anyone tell me what is causing this problem?

C get battery life under Windows 7

I'm trying to code a program that gets the % of a laptop battery and then displays a CMD showing a message (for example: 10% -> "Low battery!").
I've tried to google it, and it seems they all tried with C++ or C#.
Can anybody help me with C, please?
Edit: thanks zakinster for your reply. Shouldn't it look something like this? This code ain't working.
#include <Windows.h>
#include <Winbase.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
SYSTEM_POWER_STATUS status;
GetSystemPowerStatus(&status);
unsigned char battery = status.BatteryLifePercent;
printf("%s", battery);
}
GetSystemPowerStatus from the Win32 API should provide the information you need :
SYSTEM_POWER_STATUS status;
if(GetSystemPowerStatus(&status)) {
unsigned char battery = status.BatteryLifePercent;
/* battery := 0..100 or 255 if unknown */
if(battery == 255) {
printf("Battery level unknown !");
}
else {
printf("Battery level : %u%%.", battery);
}
}
else {
printf("Cannot get the power status, error %lu", GetLastError());
}
See the documentation of the SYSTEM_POWER_STATUS structure for a complete list of contained information.

Resources