Embedded c programming to read the devices like sensors, switches?

Thread Starter

John99407

Joined Jul 12, 2019
77
Hello,

first of all excuse me for my bad english :) I know the basics of C but I don't have any experiences with microcontroller programming. So I really hope that someone can help me :)

If we want to get the input from user we just use scanf statement to get the input from the keyboard

If I want to read the status of the switch How do we do in the programming

upload_2019-8-20_17-37-35.png

as you can see flow chart reading the state of the switch at once.

I want to understand which statement we use in embedded c programming to read the devices like sensors, switches?

I really need help! Hopefully, someone can help me!?

Much Thanks in advance!
 

danadak

Joined Mar 10, 2018
4,057
Can take several forms of solution -

1) Using a sensor that is I2C or SPI or CAN or....you use a library
function that is added to C to handle that communications interface.

2) Direct connection of sensor, the IDE used has libraries for the processor
that have f()'s to control measurement system, like the A/D, via writes/reads
to the A/D configuration registers.

For example, in this IDE each onchip component, like UART,. A/D, OpAmp,
DSP, DMA.....have a library of f() calls to manipulate that onboard resource
and external devices communicating thru some standard. You right click the
onboard component, pick datasheet, and the datasheet has a description of
each components API calls. A component in this context is an onboard resource
in the chip.

Here is an example where the project is a digital filter, I have opened up datasheet
for the A/D, looking at the API calls it has to manipulate it. Right column is resources
used onchip, as you can see many left unused. The window to its immediate left
is a catalog of the onchip resources you drag and drop onto the design canvas. As
you can see this example design does not use many of the chips resources, lots left
to use for other tasks.

See attached.

Regards, Dana.
 

Attachments

Last edited:

KeithWalker

Joined Jul 10, 2017
2,295
Hello,

first of all excuse me for my bad english :) I know the basics of C but I don't have any experiences with microcontroller programming. So I really hope that someone can help me :)

If we want to get the input from user we just use scanf statement to get the input from the keyboard

If I want to read the status of the switch How do we do in the programming

View attachment 184321

as you can see flow chart reading the state of the switch at once.

I want to understand which statement we use in embedded c programming to read the devices like sensors, switches?

I really need help! Hopefully, someone can help me!?

Much Thanks in advance!
If you have an analog or digital signal connected to an input pin on a microprocessor, you first have to assign that pin in the C program, then you can read the status of it into a variable. You can then direct the flow of the program depending on the result.
Check the programming information for the microprocessor you plan on using. Most of them give lots of programming examples.
 

MrChips

Joined Oct 2, 2009
25,919
Reading MCU (microcontroller unit) input/output pins is very straight forward.
Tell us what is your MCU and the programming platform (toolset or IDE - integrated development environment) you are using.
 

Thread Starter

John99407

Joined Jul 12, 2019
77
Reading MCU (microcontroller unit) input/output pins is very straight forward.
Tell us what is your MCU and the programming platform (toolset or IDE - integrated development environment) you are using.
Thanks, MrChips I haven't decided MCU and the programming platform but I wish to use PIC and MPLABX tool

I am using " if else statement" in the flow chart to read switch status

upload_2019-8-20_19-27-51.png


Does this flow chart read the state of a switch ?

I think Yes because if statement checks the condition true or false. state of switch

Do we always use statement (if.. else, switch case) to read the device?
 

Thread Starter

John99407

Joined Jul 12, 2019
77
It really depends on what you want to do with the switch state.

SET_SOMETHING = SWITCH_STATE;

For example:

LED = SWITCH;
Well, the flow chart is a better approach I am trying to make a flow chart that checks the switch state so if a switch is closed LED would be ON

upload_2019-8-20_20-4-7.png
 

djsfantasi

Joined Apr 11, 2010
8,255
The “if” doesn’t read the status of the switch. It checks a value, that you have obtained by reading the status of the switch .

Each MCU has a statement which reads the value of a pin. You can use it directly in an “if” statement or assign it to a variable.

I am most familiar with (Arduino) C, so let me provide a couple of examples. Let’s assume your switch is connected to GPIO pin #8 (or simply, pin 8). And let’s assume that the pin is not floating (a hardware issue, nothing to do with a flowchart). So here it goes:

To directly test if a switch is closed, you might use
Code:
if (digitalRead(8)==HIGH) {
     // do stuff
     }
else
     {
     // do other stuff
     }
To read a pin status into a variable, which I’m going to call switchState, you might do this
Code:
switchState=digitalRead(8);
if (switchState==HIGH)  {
     // do stuff
     }
else
     {
     // do other stuff
     }
 
Top