-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.c
113 lines (93 loc) · 3.29 KB
/
demo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Names: David _, David _, Jing Wei Li
Section: 4
Date: 9/15/17
File name: lab1-1
Program description:
*/
/*
This program is incomplete. Part of the code is provided as an example. You
need to modify the code, adding code to satisfy the stated requirements. Blank
lines have also been provided at some locations, indicating an incomplete line.
*/
#include <c8051_SDCC.h> // include files. This file is available online
#include <stdio.h>
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void Port_Init(void); // Initialize ports for input and output
int sensor1(void); // function which checks Pushbutton
int sensor2(void); // function that checks the Slide switch
void Set_outputs(void);// function to set output bits
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
__sbit __at 0xB6 LED0; // LED0, associated with Port 3 Pin 6
__sbit __at 0xB3 BILED0; // BILED0, associated with P3.3
__sbit __at 0xB4 BILED1; // BILED1, associated with P3.4
__sbit __at 0xB7 BUZZER; // Buzzer, associated with P3.7
__sbit __at 0xA0 SS; // Slide switch, associated with Port 2 Pin 0
__sbit __at 0xB0 PB1; // Push button 1, associated with Port 3, Pin 0
__sbit __at 0xB1 PB2; // Push button 2, P3.1
//***************
void main(void)
{
Sys_Init(); // System Initialization
putchar(' '); // the quote fonts may not copy correctly into SiLabs IDE
Port_Init(); // Initialize ports 2 and 3
while (1) // infinite loop
{
// main program manages the function calls
Set_outputs();
}
}
//***************
/* Port_Init - Initializes Ports 2 and 3 in the desired modes for input and output */
void Port_Init(void)
{
// Port 3
P3MDOUT &= 0xFC; // set Port 3 output pins to push-pull mode (fill in the blank)
P3MDOUT |= 0x98; // set Port 3 input pins to open drain mode (fill in the blank)
P3 |= ~0xFC; // set Port 3 input pins to high impedance state (fill in the blank)
// Port 2
// configure Port 2 as needed
P2MDOUT &= 0xFE;
P2 |= ~0xFE;
//
//
}
//***************
// Set outputs:
// The following code is incomplete, lighting an LED depending
// on the state of a single pushbutton.
void Set_outputs(void)
{
if (sensor2()) // if Slide Switch activated
{
LED0 = 0; // Light LED
printf("\rSlide switch is off \n");
}
else // if Slide Switch is not activated
{
LED0 = 1; // turn off LED
printf("\rSlide switch is on \n");
}
}
//***************
// Sensor - Returns a 0 if Pushbutton 1 not activated
// or a 1 if Pushbutton 1 is activated.
// This code reads a single input only, associated with PB0
// Note this code is not used by function yet, you must incorporate it
int sensor1(void)
{
if (!PB1) return 1;
else return 0;
}
//***************
// Sensor - Returns a 0 if Slide Switch is 'off'
// or a 1 if Slide switch is 'on'
// This code reads a single input only, associated with SS
int sensor2(void)
{
if (!SS) return 1;
else return 0;
}