Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,143,497 members, 7,781,500 topics. Date: Friday, 29 March 2024 at 03:34 PM

Microcontrollers - Programming (4) - Nairaland

Nairaland Forum / Science/Technology / Programming / Microcontrollers (34139 Views)

Help Needed On My Project Work Using Microcontrollers / How To Write Assembly Language For Microcontrollers (2) (3) (4)

(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) ... (14) (Reply) (Go Down)

Re: Microcontrollers by Nobody: 11:46am On Apr 29, 2013
princejude:

NOLVP => NO Low Voltage Programming

Well done
Thanks prince, but dont foget to correct and add to my explanations, dat s d esence of me trying to explain, so I can learn too!
Re: Microcontrollers by Nobody: 11:57am On Apr 29, 2013
Now to the main program!
 void main(void)
{
int16 temp_adc;
float temp;


The main class is declared and void, since it returns no value, it just does its work over and over! The other void inside (), says no value is passed to the main class(infact it can be omitted!).
lines 4 and 5, declare the variables as integer and floating point values(princejude explained this above).
Re: Microcontrollers by Nobody: 7:01pm On May 04, 2013
"yaakuri jamaa" fo d long break man must wak!
 setup_adc(ADC_CLOCK_DIV_8);
setup_adc_ports(sAN0);
set_adc_channel(0); //read anal
lcd_init();

now, d next three lines set up the ADC we hav included n our design:
the 1st line setup the clock source fo d ADC, Im not really sure but I thnk d bracket says d processors clock shuld b divided by 8 fo use by d ADC.
the 2nd line declares pin AN0 of d MCU as d analouge input pin(i really dnt kno what dat small s is fo!).
the 3rd line chooses d 1st channel of the 14 available channels as d input channel.
the 4th line initialised d lcd and prepares it fo use!
Re: Microcontrollers by Nobody: 8:54pm On May 05, 2013
 printf ( lcd_putc, "Temperature
printf ("Temperature is\n\r" ); //print
while(true)

1st printf prints to our initialised lcd, the word "Temperature is". The same word is printed to the RS232 com port by the second printf!
Our lcd has 16 columns and two rows of character, since our printf did not specify any position to start printing, the printing will start by default, at line 1, column 1!
The next line is a while loop that says, "continue this process as long as power is supplied!"
Re: Microcontrollers by princejude(m): 11:45am On May 06, 2013
cogitoErgo:
 setup_adc(ADC_CLOCK_DIV_8);
setup_adc_ports(sAN0);
set_adc_channel(0); //read anal
lcd_init();


the 2nd line declares pin AN0 of d MCU as d analouge input pin[b](i really dnt kno what dat small s is fo!).[/b]


Hello CogitoErgo, Have you installed the CCS C Compiler in your PC? If yes, open the 16f887.h file from the installation location: C/Program Files/PICC/Devices then scroll down to ADC Function setups, you will see the ADC registers definition as shown below:


// Constants used in SETUP_ADC_PORTS() are:
// First argument:
// OR together desired pins
#define sAN0 1 //| A0
#define sAN1 2 //| A1
#define sAN2 4 //| A2
#define sAN3 8 //| A3
#define sAN4 16 //| A5
#define sAN5 32 //| E0
#define sAN6 64 //| E1
#define sAN7 128 //| E2
#define sAN8 0x10000 //| B2
#define sAN9 0x20000 //| B3
#define sAN10 0x40000 //| B1
#define sAN11 0x80000 //| B4
#define sAN12 0x100000 //| B0
#define sAN13 0x200000 //| B5
#define NO_ANALOGS 0 // None
#define ALL_ANALOG 0x1F00FF // A0 A1 A2 A3 A5 E0 E1 E2 B0 B1 B2 B3 B4 B5


thats where the small 's' is from. Note that NOT ALL PIC MCUs use the small 's'.

For example the ADC register definition for 16F877A is below


// Constants used in SETUP_ADC_PORTS() are:
#define NO_ANALOGS 7 // None
#define ALL_ANALOG 0 // A0 A1 A2 A3 A5 E0 E1 E2
#define AN0_AN1_AN2_AN4_AN5_AN6_AN7_VSS_VREF 1 // A0 A1 A2 A5 E0 E1 E2 VRefh=A3
#define AN0_AN1_AN2_AN3_AN4 2 // A0 A1 A2 A3 A5
#define AN0_AN1_AN2_AN4_VSS_VREF 3 // A0 A1 A2 A4 VRefh=A3
#define AN0_AN1_AN3 4 // A0 A1 A3
#define AN0_AN1_VSS_VREF 5 // A0 A1 VRefh=A3
#define AN0_AN1_AN4_AN5_AN6_AN7_VREF_VREF 0x08 // A0 A1 A5 E0 E1 E2 VRefh=A3 VRefl=A2
#define AN0_AN1_AN2_AN3_AN4_AN5 0x09 // A0 A1 A2 A3 A5 E0
#define AN0_AN1_AN2_AN4_AN5_VSS_VREF 0x0A // A0 A1 A2 A5 E0 VRefh=A3
#define AN0_AN1_AN4_AN5_VREF_VREF 0x0B // A0 A1 A5 E0 VRefh=A3 VRefl=A2
#define AN0_AN1_AN4_VREF_VREF 0x0C // A0 A1 A4 VRefh=A3 VRefl=A2
#define AN0_AN1_VREF_VREF 0x0D // A0 A1 VRefh=A3 VRefl=A2
#define AN0 0x0E // A0


So is important to look at the header file of any chip before you can use its ADC module
Re: Microcontrollers by Nobody: 11:55am On May 06, 2013
@princejude, Im yet to get my starter package, I surely will get it today and take a look at those things. But I beleive its better to focus on one MCU first and master it very well, instead of trying to understand all MCUs at d same time? I thnk I have decided to start with 16f887!
Re: Microcontrollers by Nobody: 8:24am On May 10, 2013
@princejude, please i need ur guide on how to get the compiled program wit ccs c compiler in to the MCU!
Re: Microcontrollers by princejude(m): 10:26pm On May 11, 2013
To transfer your hex file(compiled ccs code)to your MCU, you will need the following:
1. PIC Programmer (i.e, Pickit2 or Pickit3 or ICD2 or any)Note: Pickit2 (cloned) N5500 and Pickit3 N7000
If you are using pickit2 or pikit3, install its software in your PC.
2. PIC Development kit with PI16F887
3. And of course a computer system.

I have explained how to program an MCU in my earlier post in this thread. Check from first page.
Re: Microcontrollers by Nobody: 11:59am On May 12, 2013
princejude: To transfer your hex file(compiled ccs code)to your MCU, you will need the following:
1. PIC Programmer (i.e, Pickit2 or Pickit3 or ICD2 or any)Note: Pickit2 (cloned) N5500 and Pickit3 N7000
If you are using pickit2 or pikit3, install its software in your PC.
2. PIC Development kit with PI16F887
3. And of course a computer system.

I have explained how to program an MCU in my earlier post in this thread. Check from first page.
I don wait tire fo ur reply grin but I finaly managed to get the hex file onto the MCU myself! The only problem is that the temperature reading Im getting is obviously wrong, instead of say 38 to 40 deg C, Im getting 199 to 200 deg C! I dont kno where d problem is fo now.
Re: Microcontrollers by Nobody: 12:15pm On May 12, 2013
I adjusted VR2 on the development board and that actually calibrated the temperature reading! But please Princejude, how did they get that conversion formular (i.e, temp=5.00*temp_adc*100.00/1023.00)?
Re: Microcontrollers by princejude(m): 2:34pm On May 12, 2013
cogitoErgo:
I don wait tire fo ur reply

Sorry for late reply, I was busy with office work.

cogitoErgo:
The only problem is that the temperature reading Im getting is obviously wrong, instead of say 38 to 40 deg C, Im getting 199 to 200 deg C!

Check this second line of your code:


#include <16f887.h>
#device ADC=10 /*** If your #device ADC=8 (8 is default value and is for 8 bits)
It will give you wrong values
Change it to #device ADC=10 --- for 10 bits ****/

Re: Microcontrollers by princejude(m): 3:11pm On May 12, 2013
cogitoErgo: I adjusted VR2 on the development board and that actually calibrated the temperature reading! But please Princejude, how did they get that conversion formular (i.e, temp=5.00*temp_adc*100.00/1023.00)?

That formula was used to convert the ADC value to temperature in degree Celcius

Note that the ADC module was configured to 10 bits (#device ADC=10) 10 bits = 210 = 1024 but you are counting from zero,0 to 1023
Re: Microcontrollers by usisky(m): 10:58pm On May 12, 2013
cogitoErgo: I adjusted VR2 on the development board and that actually calibrated the temperature reading! But please Princejude, how did they get that conversion formular (i.e, temp=5.00*temp_adc*100.00/1023.00)?

I think d above eqaution is a bit 'SUSPECT'. Given an ADC resolution of 10 bits , an upper and lower input voltage range of 5V and 0V. The equation should b:

Vin(temp)= (((VREFP-VREFM)/1024) *conversion_result*scaling_factor) - VREFM.

where ; VREFP--- positive refrence voltage(5V in dis case).

VREFM--- negative voltage reference(0V in dis case).

conversion result(adc value).

scaling factor(the factor by which u scaled ur input voltage i.e if u scale ur input by 100, den u multiply it by dis factor to obtain correct result).

while ur equation above will give seemingly d correct result, it wil however be off by at least 1LSB(of d ADC resolution). This is because u devided by 1023 intead of 1024. It may not appear to b a problem in such application(temperature sensing). However, when dealing with signals with narrow differential input voltages, dis wil b unacceptable.
Re: Microcontrollers by Nobody: 4:12am On May 13, 2013
usisky:

I think d above eqaution is a bit 'SUSPECT'. Given an ADC resolution of 10 bits , an upper and lower input voltage range of 5V and 0V. The equation should b:

Vin(temp)= (((VREFP-VREFM)/1024) *conversion_result*scaling_factor) - VREFM.

where ; VREFP--- positive refrence voltage(5V in dis case).

VREFM--- negative voltage reference(0V in dis case).

conversion result(adc value).

scaling factor(the factor by which u scaled ur input voltage i.e if u scale ur input by 100, den u multiply it by dis factor to obtain correct result).

while ur equation above will give seemingly d correct result, it wil however be off by at least 1LSB(of d ADC resolution). This is because u devided by 1023 intead of 1024. It may not appear to b a problem in such application(temperature sensing). However, when dealing with signals with narrow differential input voltages, dis wil b unacceptable.
Great man, Im loving NL bcause of u guys grin
Another problem, from the progrm code the thermometer seems not to be able to measure negative temp( I may be wrong though, since I have not tested it on that!), if this is true, how could I solve this? Thanks a lot!
Re: Microcontrollers by princejude(m): 6:29am On May 13, 2013
usisky:

I think d above eqaution is a bit 'SUSPECT'. Given an ADC resolution of 10 bits , an upper and lower input voltage range of 5V and 0V. The equation should b:

Vin(temp)= (((VREFP-VREFM)/1024) *conversion_result*scaling_factor) - VREFM.

where ; VREFP--- positive refrence voltage(5V in dis case).

VREFM--- negative voltage reference(0V in dis case).

conversion result(adc value).

scaling factor(the factor by which u scaled ur input voltage i.e if u scale ur input by 100, den u multiply it by dis factor to obtain correct result).

while ur equation above will give seemingly d correct result, it wil however be off by at least 1LSB(of d ADC resolution). This is because u devided by 1023 intead of 1024. It may not appear to b a problem in such application(temperature sensing). However, when dealing with signals with narrow differential input voltages, dis wil b unacceptable.

welldone for ur input, we want more contribution to this thread, let it not be one man show because am a bit busy with other programming stuffs like FPGA, ARM and PSoC
Re: Microcontrollers by hopefullman: 3:53pm On May 13, 2013
@princejude,usisky.2nks 4ur post.i(we) wish 2contribute bt we aren't up 2ur level.nt easy 2talk where our ogas are talking.pls bear with us and keep it up because u're improving lives with ur knowledge.
Re: Microcontrollers by princejude(m): 3:40am On May 14, 2013
Re: Microcontrollers by princejude(m): 4:04am On May 14, 2013
Example 4: Moving LEDs

This is another simple LED example that will ON eight LEDs sequentially(one at a time). The LEDs were connected to PORTB


#include <16f887.h>
#fuses xt, nowdt, noprotect, noput
#use delay (clock = 4000000)

void main()
{
int n; //declare n as an integer variable
while(1) // loop forever
{
output_B(0x01 << n); // ON the 1st LED and SHIFT LEFT to the next one
n++; //increase the value of n
if(n>7) //is n greater than 7 ?
n = 0; //if yes, set n equal to zero
delay_ms(400); //wait 400 ms to slow down the speed
}
}


Re: Microcontrollers by princejude(m): 4:42am On May 14, 2013
SEVEN SEGMENT DISPLAY
The segments in a 7-segment display are arranged to form a single digit from 0 to F as
shown in the figure below:



Segments are marked with non-capital letters: a, b, c, d, e, f, g and dp, where dp is the
decimal point. The 8 LEDs inside each display can be arranged with a common cathode
or common anode. With a common cathode display, the common cathode must be
connected to the 0V rail and the LEDs are turned on with a logic one. Common anode
displays must have the common anode connected to the +5V rail. The segments are
turned on with a logic zero.

To display these numbers, the following LED segments will be ON
0 = a,b,c,d,e,f
1 = b,c
2 = a,b,d,e,g
3 = a,b,c,d,g
4 = b,c,f,g
5 = a,c,d,f,g
6 = a,c,d,e,f,g
7 = a,b,c
8 = a,b,c,d,e,f,g
9 = a,b,c,f,g

Digit coding for Common Cathode



Digit coding for common anode



Example 5: 0 to 9 Counter
This sample counts from 0 to 9 on a single digit common anode 7-segment display

 
#include <16f887.h>
#fuses xt, nowdt, noprotect, noput
#use delay (clock = 4000000)

void main()
{
unsigned char i;
unsigned char num[10] = {0x40, 0x79, 0x24, 0x30, 0x19,
0x12, 0x02, 0x78, 0x00, 0x10};

output_low(PIN_A0); // the common cathode terminal was connected to PIN_A0 through a PNP transitor
// to ON the PNP you must send logic zero to its base terminal
while(true) // loop forever
{
for(i=0;i<10;i++) //count from 0 to 9
{
output_B(num[i]); //send the decoded number to portb. Note,segments a to g were connected to portb
delay_ms(300); // wait 300 ms
}
}
}

Re: Microcontrollers by princejude(m): 5:04am On May 14, 2013
EXAMPLE 6: 00 - 99 Counter

#include <16f887.h>
#fuses xt, nowdt, noprotect, noput
#use delay (clock = 4000000)

int decode(int digit)
{
int code;
switch(digit)
{
case 0: code = 0x40;break;
case 1: code = 0x79;break;
case 2: code = 0x24;break;
case 3: code = 0x30;break;
case 4: code = 0x19;break;
case 5: code = 0x12;break;
case 6: code = 0x02;break;
case 7: code = 0x78;break;
case 8: code = 0x00;break;
case 9: code = 0x10;break;
}
return code;
}

void main()
{
int i;
int units, tens ;
int Result;
while(TRUE)
{
for (i=0; i<99; i++)
{
tens = i/10;
units= i%10;

output_A(0B11111101);
Result = decode(tens);
output_B(Result);
delay_ms(100);
output_A(0B11111110);
Result = decode(units);
output_B(Result);
delay_ms(100);
}
}
}
Re: Microcontrollers by princejude(m): 5:10am On May 14, 2013
.
Re: Microcontrollers by Nobody: 1:03pm On May 14, 2013
This place: http://embedded-lab.com/blog/?page_id=26
also have a lot of examples and tutorials on PIC but its like their programs are for MikroC!
Re: Microcontrollers by Nobody: 11:08am On May 15, 2013
@princejude or anybody, please how many times can I erase and re-write PIC16f887? Please do not refer me to the datasheet, just give me the answer here! Thanks
Re: Microcontrollers by Nobody: 6:55pm On May 15, 2013
@princejude, I tried example 5 but instead of the 7-segment to come on, it was the led that was flashing!
Re: Microcontrollers by princejude(m): 2:31am On May 16, 2013
cogitoErgo: @princejude or anybody, please how many times can I erase and re-write PIC16f887? Please do not refer me to the datasheet, just give me the answer here! Thanks

You can erase and re-write more than a million times
Re: Microcontrollers by princejude(m): 2:38am On May 16, 2013
cogitoErgo: @princejude, I tried example 5 but instead of the 7-segment to come on, it was the led that was flashing!
Are you testing it with a development kit or with a soldered(vero)board? If you are using dev. kit can you post the link to the kit. We will like to know about your hardware.
Re: Microcontrollers by Nobody: 6:30am On May 16, 2013
princejude:

You can erase and re-write more than a million times





Thanks, God bless u!
Re: Microcontrollers by Nobody: 6:32am On May 16, 2013
princejude:
Are you testing it with a development kit or with a soldered(vero)board? If you are using dev. kit can you post the link to the kit. We will like to know about your hardware.
Im using the PIC dev. kit I got from microscale-embedded!
Re: Microcontrollers by princejude(m): 6:49am On May 16, 2013
cogitoErgo:
Im using the PIC dev. kit I got from microscale-embedded!

Which of the kits ? The one with on-board ISP programmer or the one without ?

The kits have a user manual that explains how to use them in a CD with some sample codes. Study the manual, there is a jumper used to enable the 7-segment and other peripherals. Maybe you only enabled the LEDs. Note that the LEDs and 7-segments are connected on the same port.
Re: Microcontrollers by Nobody: 9:54am On May 16, 2013
princejude:

Which of the kits ? The one with on-board ISP programmer or the one without ?

The kits have a user manual that explains how to use them in a CD with some sample codes. Study the manual, there is a jumper used to enable the 7-segment and other peripherals. Maybe you only enabled the LEDs. Note that the LEDs and 7-segments are connected on the same port.
Thanks. I have already gotten it working! I changed the jumper from W2 to W1 to enable the 7-segment(I did that by trial and error, noting carefully I could spoil things ).
The ICSP programmer is NOT onboard.
Re: Microcontrollers by princejude(m): 11:41am On May 16, 2013
cogitoErgo:
Thanks. I have already gotten it working! I changed the jumper from W2 to W1 to enable the 7-segment(I did that by trial and error, noting carefully I could spoil things ).
The ICSP programmer is NOT onboard.

Did you see the Pic Development Kit user manual and software (CCS C Compiler, MPLAB, Proteus) in the CD?
If yes, try all the sample codes
Re: Microcontrollers by Nobody: 4:13pm On May 16, 2013
princejude:

Did you see the Pic Development Kit user manual and software (CCS C Compiler, MPLAB, Proteus) in the CD?
If yes, try all the sample codes
Ha, which one be Proteus? I saw everytng except dat Proteus! some of d examples will nid aditional pats and I also want to build external power source fo the the breadboard which will b adapted fo final package later.
microscale said the #15000/15500 starter pack has lcd graphics display(not 16x2) but i did not see it in my package! i taught maybe its bcause i paid 15000naira!

(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) ... (14) (Reply)

First Thing First, Learn To Program! / Share Your Remote Job Experience As A Developer / How To Make A Simple Calculator In Notepad Using .bat Format

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 66
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.