Sei sulla pagina 1di 9

1. Write a function to calculate the circumference of a rectangle.

The function should


receive the two sides of the rectangle as floating point numbers and return the
circumference as a floating point number.

#define h=2
Float circum (float b, float a)
{
Float c;
c= (a+b)/h;
return c;
}

2. Write a main program to use the function you developed in Exercise 1. Find the
circumference of a rectangle whose sides are 2.3cm and 5.6cm. Store the result in a
floating point number called MyResult.

#define h=2
Float circum (float b, float a)
{
Float c;
c= (a+b)/h;
return c;
}
Void main ()
{
Float Myresult a,b,c
a=2.3
b=5.6
c=circum (a,b)
}

3. Write a function to convert inches to centimeters. The function should receive inches
as a floating point number and then calculate the equivalent centimeters.

#define y=2.54
Float convert (float inch)
{
Float cm;
Cm=y*inch
Return cm;
}
4. Write a main program to use the function you developed in Exercise 3. Convert 12.5
inches into centimeters and store the result as a floating point number.

#define y=2.54
Float convert (float inch)
{
Float cm;
Cm=y*inch
Return cm;
}
Void main ()
{
Float inch,cm
inch= 12.5
cm=convert(inch)
}
5. An LED is connected to port pin RB0 of a PIC18F452-type microcontroller through a
current limiting resistor in current sinking mode. Write a program to flash the LED in
five-second intervals.

#define LED PORTB.0


#define ON 1
#define OFF 0
#define five_second_delay_ms(5000)

Void main ()
{
TRISB = 0;
FOR (;;){
LED = ON
five_second_delay_ms(5000)

LED = OFF
five_second_delay_ms(5000)

}
}

6. Eight LEDs are connected to PORTB of a PIC18F452-type microcontroller. Write a


program so that the LEDs count up in binary sequence with a one-second delay
between outputs.

#define LED PORTB.0


#define ON 1
#define OFF 0
#define one_second_delay_ms(2000)

Void main ()
{
TRISB = 0;
FOR (;;){
PORTB++;
LED = ON
one_second_delay_ms(1000)
LED = OFF
one_second_delay_ms(1000)

}
}

7. An LED is connected to port pin RB7 of a PIC18F452 microcontroller. Write a program


to flash the LED such that the ON time is five seconds, and the OFF time is three
seconds.

#define LED PORTB.7


#define ON 1
#define OFF 0
#define three_second_delay_ms(3000)

Void main ()
{
TRISB = 7;
FOR (;;){
LED = ON
three_second_delay_ms(3000)

LED = OFF
Three_second_delay_ms(5000)

}
}

8. A text-based LCD is connected to a PIC18F452-type microcontroller in 4-bit data mode.


Write a program that will display a count from 0 to 255 on the LCD with a one-second
interval between counts.

Void main ()
{

signed int j;
int p[3]
PORTB=0
Lcd_Init(&PORTB);
Lcd_Cmd(LCD_CLEAR)
for (j=0; j<=255; j++){
Lcd_out(p[j])

delay_ms(1000)
}
}
9. A text-based LCD is connected to a PIC microcontroller as in Exercise 8. Write a
program to display the text Exercise 9 on the first row of the LCD.

Void main ()
{
TRISB=0;
Lcd_Init(&PORTB);
Lcd_Cmd(LCD_CLEAR)
Lcd_out(1,1,EXERCISE 9)

}
}

10. Repeat Exercise 9 but display the message on the first row, starting from column 3 of
the LCD.

Void main ()
{
TRISB=0;
Lcd_Init(&PORTB);
Lcd_Cmd(LCD_CLEAR)
Lcd_out(1,3,EXERCISE 9)

11. A two-row text-based LCD is connected to a PIC18F452-type microcontroller in 4-bit-


data mode. Write a program to display the text COUNTS: on row 1 and then to count
repeatedly from 1 to 100 on row 2 with two-second intervals.

char txt1[] = "COUNTS";

char i;

void main(){
TRISB=0;

Lcd_Init(PORTB);

Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,txt1);
Delay_ms(2000);
int p[3]
while(1) {
for(i=1; i<100; i++) {
Lcd_out(p[j])
}
}
}

12. Write a program to calculate the trigonometric cosine of angles from 0 to 45 in steps
of 1 and store the results in a floating point array.

Void main ()
{

unsigned charj;
doubl pi=3.14159,rads
for (j=0; j<=45; j++){
rads = j*pi/180.0;
anlge=cos(j)

}
}

13. Write a function to calculate and return the length of the hypotenuse of a rightangle
triangle, given its two sides. Show how you can use the function in a main program to
calculate the hypotenuse of a right-angle triangle whose two sides are 4.0cm and
5.0cm.

Float hipot (float b, float a)


{
Float h;
h= (a*a+b*b);
return h;
}
Void main ()
{
Float Myresult a,b,h
a=4.0
b=5.0
h=hipot (a,b)
}

14. Write a program to configure port pin RB2 of a PIC18F452 microcontroller as the
RS232 serial output port. Send character X to this port at 4800 baud.

Void main ()
{
Unsigned Char mydata =X, temp;
Soft_uart_init(PORTB,1,2,4800,0);
For(;;){
do
{
Temp =Sfot_uart_read(&mydata);
}
While (mydata)
Temp++
Soft_uart_write(temp);
}

15. Port RB0 of a PIC18F452 microcontroller is configured as the RS232 serial output port.
Write a program to send out string SERIAL at 9600 baud.

Void main ()
{
Unsigned Char *p =SERIAL, temp;
USart_init(9600);
For(;;){
do
{

While (Usart_data_ready());
Temp=usart_read();
Temp++
Soft_uart_write(Temp);
}

16. Repeat Exercise 15 but use the hardware USART available on the microcontroller chip.
Void main ()
{
Unsigned Char *p =SERIAL, temp;
USart_init(9600);
For(;;){
do
{

While (Usart_data_ready());
Temp=usart_read();
Temp++
Soft_uart_write(Temp);
}
17. Explain the differences between software-implemented serial data communication
and USART hardware-based serial communication.
El software es una transmisin atreves de un programacin y el hardware transmite
apatir de un parte fsica que tiene el microcontrolador.

18. Write a function to add two arrays are function as arguments. Store the sum in one of
these arrays.

19. Write a function to perform the following operations on two-dimensional matrices:


a) Add matrices
*/X=como un numero opcional para el programador*/
Float add (float p[x][x], float q[x][x]){

(Float R[x][x];
Int j;
Int i;
For(i=0,i<=x,i=++){
For(j=0,j<=x,j=++){
R[i][j]=p[i][j]+w[i][j];
Return(R[x][x]);
}
b) Subtract matrices
*/X=como un numero opcional para el programador*/
Float sub (float p[x][x], float q[x][x]){

(Float R[x][x];
Int j;
Int i;
For(i=0,i<=x,i=++){
For(j=0,j<=x,j=++){
R[i][j]=p[i][j]-w[i][j];
Return(R[x][x]);
c) Multiply matrices
*/X=como un numero opcional para el programador*/
Float mult (float p[x][x], float q[x][x]){

(Float R[x][x];
Int j;
Int i;
For(i=0,i<=x,i=++){
For(j=0,j<=x,j=++){
R[i][j]=p[i][j]*w[i][j];
Return(R[x][x]);
20. Write a function to convert between polar and rectangular coordinates.
Float radians (float a)
{
Float rad;
rad= (a*3.1415/180.0);
return rad;

Float ejex (float r, float thetha )


{
Float x,rad;
Rad=radians(theta)
x= (r*cos(rad));
return x;
}
Float ejey (float r, float thetha )
{
Float y,rad;
Rad=radians(theta)
y= (r*sin(rad));
return y;
}

Void main ()
{
Float x,y,r,theta
Theta=
r=
x=ejex (r, thetha);
y=ejey (r, thetha);
}

21. Write functions to convert temperature expressed in Celsius to Fahrenheit and vice
versa. Show how these functions can be called from main programs to convert 20C toF
and also 100F toC.

Float celtofa (float ce )


{
Float fa;
fa= ((9/5)*ce)+32;
return fa;
}
Float fatoce (float f )
{
Float c;
c= ((f-32)*(5/9));
return c;
}
Void main ()
{
Float ce,c,fa,f
ce=20;
f=100;
fa=cetofa (ce);
c=fatoce (f);
}

22. Write a program to store the value of function f(x) in an array as x is varied from 0 to
10 in steps of 0.5. Assume that: fx1:3x3
2:5
x2
3:1x
4:5

Potrebbero piacerti anche