If you've programmed in Arduino and now you're programming in mikroC, most likely have many commands you to take less, as "analogRead ()" and Serial.print ().Well, in this tutorial we will cover how to read analog signals and how a console us, to debug our projects.
It is important that if you do not know how to program in mikroC and have only programmed in Arduino, then it is required that you read my first post , otherwise you will not know what we're talking about, if you know, did not say anything.
Before we begin, let's clarify some things. This post will deal an ATmega168 (You can occupy both the 168 and the 328, all the same).This micro, unlike the ATtiny13 or 2313, you need an external crystal, so one of the things you should do from the start (now) is to place the crystal on pins 9 and 10, gives the same position. NOTE : you can take the Atmega without glass, but so we must burn the fuses. The best thing for now is to occupy a Crystal The second thing you should do immediately is to connect the pin 21-5 volts. This pin is the reference voltage and the voltage according to which it is connected, it is the result of the analog reading. Once made these 2 things can continue ...
1. How do I analogRead() in MikroC ?
The function that we know as analogRead Arduino () in MikroC the call ADC_Read (x). ADC comes from Analog Digital Converter and it is important that every time you invoke this function get fixated on the datasheet of your micro AVR, as there are some that have 2 ADC ports, others have 5 (Atmega168, Atmega328) and others who have many more .
Another important point is that before occupying the ADC_Read (x) function, you must put as the first line of your code, the following line
#include <built_in.h>
Do not forget that the ADC option must be enabled, for that fijate the tabs on the right side, one of these tabs is called "Library Manager", click and find the ADC option, if their respective box has no check, you mark it It must be checked.
The only thing missing for me is explain the "x" and notice that the pin 23,24,25,26,27 and 28 have a label that says ADCX, this X is the number of ADC converter assigned, in this case the Atmega168 and 328 have 6 ADC. The first is the ADC0 and the last is the ADC5. So if you want to read a signal that is connected to pin 26, you must do ADC_Read (3), Claro? Perfect.
▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
unsigned int i;
void main()
{
char cadena[8]; // definimos un arreglo de 8 espacios
UART1_Init(9600); // definimos la velocidad a 9600 bps
int numero=12345678; // este número lo queremos transformar a chars
while(1)
{
IntToStr(numero, cadena); // esta función transforma el numero a chars
for (i = 0; i <7 ; i++ )
UART1_Write(valor[i]);
UART1_Write_Text(" "); // dejo un pequeño espacio entre cada cifra
delay_ms(1000);
}
}
▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
2. How do Serial.print() in MikroC ?
This is another of the features that make us finish our projects in Arduino, the possibility of going debuggeando our projects, as well, in MikroC we can do the same, but often we forget or avoid because it has a difficulty bonus the first time that deal.
First things first, remember that in Arduino in the void setup do "Serial.begin (9600)," well, here will do the same, here we occupy "UART1_Init (9600)". The objective of this line is to tell the serial communication chip that will be 9600 bits per second.
To write to the console have 2 options ...
UART1_Write_Text ( "put your text here"): With this command you write a user-defined text.
UART1_Write (char [i]): With this command write a char that was defined within the code. This is the equivalent of Serial.print () command, but the difference is that here we can not enter an integer or a result, this function only accepts char's, therefore, if we know that value gives us a function, we pass the number (int, long, double) to a string and then send each char for separately from the console.
Here you also go to Library Manager and check the UART option
To move from int to string is super easy. Suppose I have the following value
int val = 12345678
then what I want is to define a string which will have 8 chars, and each char will each character of our number. The first thing we do is define the arrangement of chars as well
char string [8];
now we
IntToStr (val, string);
And presto, now every char has a character of our number. It is important to note that these arrangements are "zero indexed", ie, the first is char string [0]. When the first char is chain [1] is called "one indexed" but it is not our case.
Perfect, now takes the FTDI cable and fijate having 6 cables.
The black cable must connect to the land of our circuit (GND) and the yellow wire should go to pin TX (transmitter) our chip, which in the atmega168 / 328 corresponds to the pin 3.
Connect the cable to the computer FTDI fijate COM port assigned to your computer, now opens the hyperterminal and initiates a connection on the same port number that was assigned to the FTDI cable and defines the rate at 9600 bits / second.
If you have Windows Vista or Windows 7, then you will notice that the HyperTerminal is no longer, but you can download from here
Almost finished, it's time to arm the wiring
Eye, red plate serves the same function as the FTDI cable there are many replacement options for the cable, you choose the one that will be easier to achieve, the important thing here is that the land go connected to both the cable or plate next the chip, ie have common ground, and the other important thing is that the Tx pin of your chip must go to the Rx pin of your board or FTDI cable.
Usually, it is time to load our final code . Click here, copy it , paste itand load it with our programmer.
The video shows that by varying the voltage with potentiometer, this voltage is measured by the ADC of our chip and then this value is sent to our computer via FTDI cable.
In conclusion, as I said in the video. Arduino is much faster when viewing results in console and other things, but once you know this trick, everything is easier. In addition the program in MikroC grace is that you can work with any chip on the AVR family and this will give you an advantage when faced with a project that goes over one Atmega328 capabilities.
Comments are welcome Greetings!
Posted by ElectricShock
:
잡동사니들(지극히 개인취향인...) (다른글에도 댓글 부탁해요♥)
You May Leave English Messages on GuestBook. by ElectricShock