Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,439 members, 7,819,620 topics. Date: Monday, 06 May 2024 at 07:16 PM

Microcontrollers - Programming (3) - Nairaland

Nairaland Forum / Science/Technology / Programming / Microcontrollers (34318 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: 8:00pm On Apr 01, 2013
@princejude and holax, thanks a lot for ur responses. Im also interestd n dis PIC stuff as I wuld like to have an automatic weather station dat culd datalog, etc!
but im currently financialy weak and surely i will monitor dis thread and infact im savng every page of it and when im ok, i will like help on how to get some of d hardwares!
Re: Microcontrollers by Nobody: 9:01pm On Apr 01, 2013
hopefullman:
thank u.must i av 2learn asm b4 learnin c?i ws tld dt asm wl let me undstand d pic 4beta resourc alocatn.pls u are an xpert in ds fild pls shed mor lit on d 2/3 langs.i dnt wnt 2spend so much tym on a lang dt wunt realy benefit me much.2nks

I think Mr. princejude answered this very well. Just to second him, I think you should learn C. Note that code written in assembly language may be a little faster than that written in C, but for the vast majority of applications, you do not need that extra little speed. What matters is that your code meets deadlines, which in approximately all cases it would when written in C.
The major problem with assembly language appears when you have to debug your code. It is very very easy to get confused especially if your code has many lines. Secondly, if you have to switch to another family of microcontrollers, say atmel or avr, you would need to learn almost entirely new instruction sets and processor architectures. With C, you'd do far less work.

Finally, code written in C is usually shorter than that written in assembly language and far more easily understandable.
For example, the C equivalent of the assembly code you wrote is:

void main()
{
TRISA = b'00000'; //set PORTA to be an output PORT
PORTA = b'11111'; //send 5volts to all pins of PORTA (RA0 - RA4);
while(1) PORTA = ~PORTA; //keep alternating between 5Volts and 0volts on RA0 - RA4
}

I advice that you should learn to comment your codes. This will save you a lot of hard work understanding what you did if after a long time you revisit the code.
Re: Microcontrollers by princejude(m): 11:13am On Apr 02, 2013
cogitoErgo: I wuld like to have an automatic weather station dat culd datalog, etc!
That will be a nice project

cogitoErgo:
i will like help on how to get some of d hardwares!
You will get most of the hardware at www.jutronix.com
Re: Microcontrollers by hopefullman: 1:28pm On Apr 03, 2013
@tesla&princ jude.thanks very muck 4ur answers
Re: Microcontrollers by princejude(m): 8:01am On Apr 04, 2013
hoodboi: hi princejude, thank you so much for ur efforts, it is highly appreciated. I want to ask, is there any book in pdf format you can upload to teach programming micro controllers. i downloaded a mplab compiler recently and i now have the proteus simulator. Also, pls among the exercises, can you include one to control the to and fro movement of a motor. thanx

Exercise 3 : Servo motor control.
In this exercise, I will copy some sections of my code in a recent project titled "Automated waste bin". This is a simple wast bin with some automatic controls. The waste bin have an ultrasonic sensor, if you come close to the bin, the bin's door will open and ones you are through and out from the bin, the door will close. The opening and closing of the door was controlled using a servo motor while your distance from the bin was measured using an ultrasonic sensor.


Chei !! i don go late ooo ... will be back shortly
Re: Microcontrollers by Nobody: 3:02pm On Apr 04, 2013
princejude:


Chei !! i don go late ooo ... will be back shortly
heiyaa, sorry u go get favour on our behalf, abi u be d oga at the top in your working place?
Re: Microcontrollers by Nobody: 3:27pm On Apr 04, 2013
princejude:
That will be a nice project


You will get most of the hardware at www.microscale-embedded.com
The website is not opening!
Re: Microcontrollers by Nobody: 8:36am On Apr 06, 2013
cogitoErgo:
The website is not opening!
The website is working now!
Please why is everybody not talking again, where is Princejude and co? I have been on microscale website, but Im still enjoying all the contributions here!
Please OP, I will be very happy if you can change the title to "Microcontroller for Beginners". And personally, I like the emphasis on C language rather than Assembly language!
Re: Microcontrollers by hopefullman: 11:28pm On Apr 06, 2013
@princjud.pls is it 2ru dt basic is more simpler than C?can i get a link 2learn Basic.thank u
Re: Microcontrollers by princejude(m): 9:33am On Apr 08, 2013
hopefullman: @princjud.pls is it 2ru dt basic is more simpler than C?can i get a link 2learn Basic.thank u

Basic may be more simpler but you will get more online resources/tutorials on C. if you want to learn basic check mikroBasic at mikroElectronika. http://www.mikroe.com/mikrobasic/pic/
Re: Microcontrollers by princejude(m): 9:57am On Apr 08, 2013
cogitoErgo:
Please why is everybody not talking again, where is Princejude and co?

I was busy,will try to upload some today
Re: Microcontrollers by princejude(m): 11:34am On Apr 08, 2013
princejude:

Exercise 3 : Servo motor control.
In this exercise, I will copy some sections of my code in a recent project titled "Automated waste bin". This is a simple wast bin with some automatic controls. The waste bin have an ultrasonic sensor, if you come close to the bin, the bin's door will open and ones you are through and out from the bin, the door will close. The opening and closing of the door was controlled using a servo motor while your distance from the bin was measured using an ultrasonic sensor.


Chei !! i don go late ooo ... will be back shortly

Yeah am back...
Here is the code for the servo motor control:


#include <16F887.h>
#device adc=8
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=cool

#define TRIG PIN_B2 // HC-SR04 Ultrasonic trig pin connected to MCU's pin B2
#define ECHO PIN_B1 // HC-SR04 Ultrasonic echo pin connected to MCU's pin B1
#define SERVO PIN_C3 // Servo motor connected to MCU's pin C3
#define LOW_BAT PIN_A1 // Low battery indicator connected to pin A1
#define FULL PIN_A2 // Waste bin FULL indicator connected to pin A2
#define RX PIN_B3 // Photo transistor(used to detect when waste-bin is full)
// is connected to pin B3

int16 distance, time, adc_res; // declare some variables as int16

void pulse_clockwise() // function to turn servo motor clockwise
{
output_high(SERVO);
delay_us(1445);
output_low(SERVO);
delay_ms(20);
}

void pulse_anticlockwise() // function to turn servo motor anticlockwise
{
output_high(SERVO);
delay_us(1530);
output_low(SERVO);
delay_ms(20);
}

void stop_pulse() // function to stop servo motor
{
output_high(SERVO);
delay_us(1480);
output_low(SERVO);
delay_ms(20);
}

void open_bin() // funtion to open the waste-bin
{
int8 i;
for(i=0;i<8;i++) // turns servo motor clockwise eight times
{
pulse_clockwise();
delay_ms(50);
stop_pulse();
delay_ms(400);
}
}

void close_bin() // function to close waste-bin
{
int8 j;
for(j=0;j<8;j++) // turns servo motor anticlockwise eight times
{
pulse_anticlockwise();
delay_ms(50);
stop_pulse();
delay_ms(400);
}
}

int16 get_distance() // gets your distance from the waste-bin(using HC-SR04
// Ultrasonic sensor
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4); // initiating timer
{
output_high(TRIG); // ping the sonar
delay_us(20); // sending 20us pulse
output_low(TRIG);
while(!input(ECHO)) // wait for high state of echo pin
{}
set_timer1(0); // setting timer zero
while(input(ECHO)) // Wait for high state of echo pin
{}
time=get_timer1(); // Getting the time
distance=time*0.028 + 1.093 ; // Calculating the distance
return (distance); // returns the value of distance
}
}

void get_adc() // Sense the battery level using the internal
// ADC(Analog to Digital Converter) module of
// the microcontroller
{
delay_ms(50); // wait for ADC to initialize
adc_res = read_adc(); // Reads the adc result
if(adc_res < 200)output_high(LOW_BAT); // If the ADC result is below 200,
// ON the low battery indicator
else output_low(LOW_BAT); // if not,OFF the low bat indicator
}

void main() // main program starts here
{
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(AN0);
set_adc_channel(0); // Selects adc channel 0
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);

// TODO: USER CODE!!

stop_pulse(); // Stop servo motor
output_low(SERVO);
output_low(FULL);
output_low(LOW_BAT);
stop_pulse();

while(true)
{
if(input(RX)){output_high(FULL);} // if waste-bin full,ON ful indicator
else {output_low(FULL);} // else OFF indicator
get_adc(); // gets adc-value(battery level)
get_distance(); // gets your distance from the waste-bin
if(distance < 30) // if your distance to the bin is less than 30cm
{
open_bin(); // Open waste-bin
stop_pulse();
delay_ms(5000); // waits 5 second for you to dispose your waste
close_bin(); // Close waste-bin
stop_pulse();
}
else
{
stop_pulse(); // Stop servo motor
}
}
}
Re: Microcontrollers by Nobody: 7:19pm On Apr 08, 2013
@princejude: Nice work. Very readable code. BTW how did you ensure the blocks were indented. The last time I tried to indent code on nairaland I was unsucessful.
Re: Microcontrollers by Nobody: 8:20pm On Apr 08, 2013
princejude:

I was busy,will try to upload some today
You are wellcome back. Thanks!
Re: Microcontrollers by hoodboi(m): 11:09am On Apr 09, 2013
Very nice code,vry readable. Hw did u knw how to work wit d sensor nd oda components nd also hw did u knw d level to set for d battery
Re: Microcontrollers by princejude(m): 2:01pm On Apr 09, 2013
NeoTesla: @princejude: Nice work. Very readable code. BTW how did you ensure the blocks were indented. The last time I tried to indent code on nairaland I was unsucessful.

Use the code icon labelled "#"

Before you type the code you want to post,Click the "#" icon and type the code between
 and 
.

[.code]

type your
code here

[./code]
Re: Microcontrollers by princejude(m): 2:11pm On Apr 09, 2013
hoodboi: Hw did u knw how to work wit d sensor nd oda components

I will always study there datasheets.

hoodboi: hw did u knw d level to set for d battery

I used the internal Analog to Digital Converter module of the chip to sense the battery level ans also used a variable resistor to calibrate the battery level.
Re: Microcontrollers by Nobody: 7:18pm On Apr 10, 2013
@ princejude;
Ok, I see. Thanks.
Re: Microcontrollers by Nobody: 8:49am On Apr 13, 2013
@princejude, please assuming I have a project to design and construct a PIC temperture sensor that will have an analog temperature sensor connected to the PIC. The PIC will sense the ambient temperature and if it above a preset value, a red light(LED) will light, showing danger! If it is within certain range, a green light will show, indicating normal! If it is lower than another preset value, a yellow light will show, indicating temperature too low. Please, how do you guide on this project? Thanks.
Re: Microcontrollers by hopefullman: 12:28am On Apr 15, 2013
@princejude.u are a 'wizard' d code is jst lyk chinese 2me.i jst wish 2 be lyk u.keep it up.thank u 4 al ur response
Re: Microcontrollers by princejude(m): 1:05pm On Apr 15, 2013
.
Re: Microcontrollers by princejude(m): 1:55pm On Apr 15, 2013
hopefullman: ...d code is jst lyk chinese 2me.i jst wish 2 be lyk u.

Did you download the two ebooks I posted their links ? if yes, study them and you will understand the trick
Re: Microcontrollers by Nobody: 7:43am On Apr 19, 2013
princejude:

Check this link: http://www.microscale-embedded.com/blog/?p=164 for a working sample on temperature measurement using LM35 temperature sensor.
Thanks, but I was actually thinking of seen a teaching and learning thing, instead of ready made code!
Could the tutorial on the microscale blog be arranged in such a way that u will start with the simplest program, such as blinking an LED. The code should be explained line by line and the use of CCS C compiler to compile the code explained. The retreival and burning of the HEX code into the PIC module should be explained.
Subsequently, more harder and useful projects should then be given with emphasis laid on explaining the software code!
I beleive that is what is called a tutorial!
Re: Microcontrollers by hopefullman: 5:03am On Apr 20, 2013
cogitoErgo:
Thanks, but I was actually thinking of seen a teaching and learning thing, instead of ready made code!
Could the tutorial on the microscale blog be arranged in such a way that u will start with the simplest program, such as blinking an LED. The code should be explained line by line and the use of CCS C compiler to compile the code explained. The retreival and burning of the HEX code into the PIC module should be explained.
Subsequently, more harder and useful projects should then be given with emphasis laid on explaining the software code!
I beleive that is what is called a tutorial!
gbam!!!1000 likes.xactly my thaught.it's nt easy 2undstand dcode without line by line xplanatn.i no big prjcts lyk ds cnt be easily xplaind bt smple prjcts cn easily be xplaind.we're stl hopin 4d bst
Re: Microcontrollers by Nobody: 7:44am On Apr 20, 2013
@princejude, please in ur variable declaration I saw <int16>. Please which kain declaration be this? I only know <int> as in integer variable!
Thanks
Re: Microcontrollers by Nobody: 12:13pm On Apr 20, 2013
cogitoErgo: @princejude, please in ur variable declaration I saw <int16>. Please which kain declaration be this? I only know <int> as in integer variable!
Thanks
OK, @princejude, from my findings on the net, integer variable in ccs c is a little different frm traditional c language! the normal int will declare a 8-bit integer, int16 will declare a 16-bit integer while int32 will do fo 32 bit integer!
Re: Microcontrollers by princejude(m): 11:42am On Apr 22, 2013
cogitoErgo:
OK, @princejude, from my findings on the net, integer variable in ccs c is a little different frm traditional c language! the normal int will declare a 8-bit integer, int16 will declare a 16-bit integer while int32 will do fo 32 bit integer!

You can also declare 8-bit interger as int8 in ccs c

Example of some data types are:


Data types Unsigned
int1 0 or 1
int8 0 to 255
int16 0 to 65535
int32 0 to 4294967295


You can download this ccs c user manual for more info:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Re: Microcontrollers by Nobody: 9:17pm On Apr 22, 2013
princejude:

You can also declare 8-bit interger as int8 in ccs c

Example of some data types are:


Data types Unsigned
int1 0 or 1
int8 0 to 255
int16 0 to 65535
int32 0 to 4294967295


You can download this ccs c user manual for more info:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Thanks for the manual. I think what remains for me is to get the PIC development kit!
Re: Microcontrollers by princejude(m): 5:36pm On Apr 24, 2013
cogitoErgo:
Thanks for the manual. I think what remains for me is to get the PIC development kit!
When you are ready for the PIC development kit, check this link http://www.jutronix.com
They have good development kits with user manual which include many sample codes for beginners.
Re: Microcontrollers by Nobody: 1:49pm On Apr 28, 2013
In order to enhance my own learning, I wish to explain the code below, line by line and I wish the professionals such as princejude will be around to correct me anytime I err! The code is from microscale site for temperature sensor. princejude provided the link as above! Please note that Im neither a pro nor any guru here, I am just a bloody learner!
 #include <16f887.h>
#device ADC=10
#use delay(clock=4000000)
#fuses XT, NOWDT, NOPROTECT, NOLVP
#use RS232(baud = 9600, xmit = PIN_C6, rcv = PIN_C7)
#include <lcd.c>
void main(void)
{
int16 temp_adc;
float temp;
setup_adc(ADC_CLOCK_DIV_8);
setup_adc_ports(sAN0);
set_adc_channel(0); //read analog input from channel 0
lcd_init();
printf ( lcd_putc, "Temperature is\n" ); //print to lcd
printf ("Temperature is\n\r" ); //print to serial com
while(true)
{
delay_ms(10);
temp_adc = read_adc();
temp = 5.00*temp_adc*100.00/1023.00; // = temp_adc*0.48828125;
// temp = temp_adc*0.48828125;
lcd_gotoxy(5,2);
printf ( lcd_putc,"%5.1f", (float)temp);
lcd_putc(223); //this number 223 will display the degree sign
lcd_putc('C');
printf ("%5.1f", (float)temp);
putc(223);
putc('C');
printf ("\n\r"wink;
delay_ms(5000);
}
}
Re: Microcontrollers by Nobody: 2:41pm On Apr 28, 2013
Now to work
 #include <16f887.h>
#device ADC=10
#use delay(clock=4000000)
#fuses XT, NOWDT, NOPROTECT, NOLVP
#use RS232(baud = 9600, xmit = PIN_C6,
#include <lcd.c>


The first line is a c pre-processor, telling the compiler to load all necessary file relating to the PIC 16F887 module. Anybody who has some knowledge of c language should know what a pre-processor is!

Second line. Since our operation is to take a signal from outside in an analogue form and pass it to the MCU for processing then a digital to analoguo converter (ADC) is needed! So d second line specifies an ADC with 10 bit data capacity( d bit capacity of an MCU can always be obtained from d MCU datasheet which is freely available on d net).

3rd line specifies the clock frequency od the PIC module, here 4MHz. This is needed im order for d compiler to calculate the required number of MCU cycles.

Next 4th line: It specifies the states of the comfiguration fuses that should be programmed onto d MCU.
Here: XT = > clock is an internal crystal oscillator.
NOWDT => No WatchDog Timer.
NOPROTECT => Code protection is off!
NOLVP => I really could not get this!

5th line says we are going to use RS232 for communicating with something like your computer(u can leave dis out if only u wish ur output on lcd!)
Here, the baud rate (somebody should explaing this!) is set at 9600.
xmit = PIN_C6, sets pin C6 of the PIC as the output pin(xmit = transmit) for d RS232.
rcv = PIN_C7, sets pin C7 as the input pin (rcv = receive).

Line 6: tells d compiler to include files for working with liquid crystal display (lcd).
Re: Microcontrollers by princejude(m): 8:41am On Apr 29, 2013
cogitoErgo:

NOLVP => I really could not get this!


NOLVP => NO Low Voltage Programming

Well done

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

What Is Computer Programming / First Thing First, Learn To Program! / 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. 62
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.