0% found this document useful (0 votes)
46 views10 pages

MSP430 GPIO and Sensor Control Code

The document is a C program for the MSP430G2553 microcontroller that interfaces with various sensors and displays data on an OLED screen. It includes functionalities for reading light and rain sensors, controlling a motor, and displaying time and temperature. The program also supports user input for setting alarms and operates in both automatic and manual modes.

Uploaded by

Tuyền Lê
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views10 pages

MSP430 GPIO and Sensor Control Code

The document is a C program for the MSP430G2553 microcontroller that interfaces with various sensors and displays data on an OLED screen. It includes functionalities for reading light and rain sensors, controlling a motor, and displaying time and temperature. The program also supports user input for setting alarms and operates in both automatic and manual modes.

Uploaded by

Tuyền Lê
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include "msp430g2553.

h"
#include "string.h"
#include "string.h"
#include "delay.h"
#include "delay.c"
#include "HW_I2C.c"
#include "UART.h"
#include "lcd_i2c.h"
#include "Font.h"
#include "ds1307.h"
#include "sh1106.h"
#include "images.h"
#include "ds18b20.h"

//===DINH NGHIA CAC CHAN GPIO ============


//====PORT1============
#define LDR_SENSOR BIT4 // P1.0 - Light sensor (ADC)
//====PORT2============
#define CHE_DO BIT0 // P2.0 - Mode switch button
#define DAO_CHIEU BIT1 // P2.1 - Direction switch button
#define SW1 BIT2 // P2.2 - Switch 1
#define SW2 BIT3 // P2.3 - Switch 2
#define RAIN_SENSOR BIT4 // P2.4 - Rain sensor input
#define BTS_IN1 BIT6 // P2.6 - Motor control IN1
#define BTS_IN2 BIT7 // P2.7 - Motor control IN2

//=== KIEM TRA TRANG THAI NUT NHAN SW1 VA SW2=========


#define SWITCH1 (P2IN & SW1)
#define SWITCH2 (P2IN & SW2)

//DINH NGHIA CAC TRANG THAI


#define SANG 0 //Trang thai troi sang
#define TOI 1 //Trang thai troi toi
#define MUA 0 //Trang thai thoi tiet mua
#define NANG 1 //Trang thai thoi tiet nang
#define AUTO 0 //Che do hoat dong tu dong
#define HAND 1 //Che do hoat dong bang tay

//KHOI TAO BIEN TOAN CUC


int anhsang, troi, thoitiet; // Luu tru trang thai moi truong
int auto_hand = AUTO; // Che do tu dong/tay auto, khoi tao la auto
int vao_ra = 0; //trang thai keo vao/ra. 0 la keo ra, 1 la keo vao
//CHUONG TRINH CON
void init_GPIO();
void ngat();
void init_adc_ldr();
void doc_ldr();
void trang_thai_anh_sang();
void trang_thai_mua();
void keo_vao();
void keo_ra();
void dung_dc();
void control();

//========================================
char time[] = " : ";
char calendar[] = " / /20 ";
char clear_display = 0;
char arr[5];
char arr1[5];
unsigned char hour_hen = 255; // 255 = chua h?n
unsigned char min_hen = 255;

char chuoi[20], hienthi[8];


unsigned char uart_index = 0;
unsigned char chuoi_ready = 0;

const unsigned char alarm_icon_16x8[] = {


0x18, 0x24, 0x42, 0x81, 0x99, 0xA5, 0x81, 0x81,
0x81, 0xA5, 0x99, 0x81, 0x42, 0x24, 0x18, 0x00
};

// Bi?u tu?ng d?ng h? 8x8 pixel


const unsigned char clock_icon_8x8[8] = {
0x3C, // 00111100
0x42, // 01000010
0x91, // 10010001
0x89, // 10001001
0x85, // 10000101
0x42, // 01000010
0x3C, // 00111100
0x00 // 00000000
};

void OLED_drawImage1Page(unsigned char col, unsigned char page, const unsigned char *image,
unsigned char width)
{
OLED_setXY(col, col + width - 1, page, page);
for (int i = 0; i < width; i++)
{
I2C_USCI_write_byte(0x40, image[i]);
}
}

void OLED_drawClockIcon(unsigned char col, unsigned char page, unsigned char size)
{
OLED_setXY(col, 127, page, page);

switch(size) {
case 8:
for(int i = 0; i < 8; i++) {
I2C_USCI_write_byte(0x40, clock_icon_8x8[i]);
}
break;

}
}

void ftoa(char* string,int a,int b)


{
string[0]=a/1000+0X30; //ngan // 0X30=48 THAP PHAN
string[1]=(a%1000)/100+48; // tram
string[2]=((a%1000)%100)/10+48; //chuc
string[3]=((a%1000)%100)%10+48; //donvi
if(b!=0)
string[4]=44;
string[5]=b/1000+0X30;//ngan // 0X30=48 THAP PHAN
string[6]=(b%1000)/100+48; // tram
string[7]=((b%1000)%100)/10+48; // chuc
string[8]=((b%1000)%100)%10+48; // donvi
}
void ftoa_1(char* string, unsigned int a)
{
string[0]=((a%1000)%100)/10+48; //chuc
string[1]=((a%1000)%100)%10+48; //donvi
}

void convert_time(void)
{
//time[7] = (sec % 10) + '0';
//time[6] = (sec / 10) + '0';
time[4] = (min % 10) + '0';
time[3] = (min / 10) + '0';
time[1] = (hour % 10) + '0';
time[0] = (hour / 10) + '0';

calendar[9] = (year % 10) + '0';


calendar[8] = (year / 10) + '0';
calendar[4] = (mon % 10) + '0';
calendar[3] = (mon / 10) + '0';
calendar[1] = (date % 10) + '0';
calendar[0] = (date / 10) + '0';
}
// Tr? v? 1 n?u pin ? m?c cao (HIGH), 0 n?u pin ? m?c th?p (LOW)
int check_pin_state(unsigned char port, unsigned char pin) {
switch(port) {
case 1: // Port 1
return (P1IN & (1 << pin)) ? 1 : 0;
case 2: // Port 2
return (P2IN & (1 << pin)) ? 1 : 0;
default:
return -1; // Port không h?p l?
}
}
void display_pin()
{
I2C_USCI_init(0x27);
lcd_set_cursor(0,0);
lcd_print("P0P1P2P3P4P6P7");
lcd_set_cursor(1,0);

lcd_print_int(check_pin_state(2,0));
lcd_print_int(check_pin_state(2,1));
lcd_print_int(check_pin_state(2,2));
lcd_print_int(check_pin_state(2,3));
lcd_print_int(check_pin_state(2,4));
lcd_print_int(check_pin_state(2,6));
lcd_print_int(check_pin_state(2,7));
}
void nhiet_do()
{
int data_ds18 =(int)DS18B20();
ftoa_1(arr,data_ds18);

OLED_printTextBold(92, 0, arr);
OLED_printTextBold(117, 0, "C");
}
void as()
{
OLED_setXY(0, 0x7F, 5, 5);
ftoa_1(arr,anhsang);
OLED_Data(arr);
__delay_cycles(100);
OLED_setXY(0, 0x7F, 6, 6);
ftoa(arr1,ADC10MEM,0);
OLED_Data(arr1);
}
void thoigian(void)
{
convert_time();
char *days_of_week[] = {"CN", "T2", "T3", "T4", "T5", "T6", "T7"};
I2C_USCI_init(0x3C);
OLED_setXY(0, 0x7F, 1, 1);
OLED_Data(" ");
//OLED_Data(days_of_week[day - 1]);
OLED_printTextBold(0,0,days_of_week[day - 1]);
OLED_drawImage1Page(30, 0, alarm_icon_16x8, 16); // V? icon ? c?t 0, hàng page 0

OLED_drawClockIcon(0, 7, 8);
//OLED_Data(time);
OLED_printTextBold(30,1,time);
//OLED_printTextBold_dao(7,time);
//OLED_printTextBold2(30,7,time);
// OLED_setXY(0, 0x7F, 2, 2);
OLED_printTextBold2(27,2,calendar);

}
//==============================================
void display_sys()
{
thoigian();
nhiet_do();
as();
display_pin();

}
//===============================================
void lay_7_ky_tu_cuoi(char *src) {
int len = strlen(src);
if (len >= 5) {
strncpy(hienthi, &src[len - 5], 5);
hienthi[5] = '\0'; // k?t thúc chu?i
} else {
strcpy(hienthi, src); // n?u chu?i ng?n hon 5, copy toàn b?
}
}
void tach_gio_hen(char *str) {
if (strlen(str) >= 5 && str[2] == ':') {
hour_hen = (str[0] - '0') * 10 + (str[1] - '0');
min_hen = (str[3] - '0') * 10 + (str[4] - '0');
}
}
void hen_gio()
{
if (chuoi_ready) {
I2C_USCI_init(0x3C); // OLED address
OLED_clear();
OLED_setXY(0, 0x7F, 4, 4);
OLED_Data("Hen gio ");
lay_7_ky_tu_cuoi(chuoi);
tach_gio_hen(hienthi);
OLED_Data(hienthi); // Hi?n th? chu?i nh?n du?c

uart_puts("\r\n Da dat lich hen thanh cong ");

chuoi_ready = 0; // Reset c?
}
if (hour == hour_hen && min == min_hen) {
uart_puts("Da den gio hen !");
hour_hen = 255; // reset d? không g?i l?i nhi?u l?n
I2C_USCI_init(0x3C); // OLED address
OLED_clear();
}
}
///===========================================
//Chuong trinh chinh
void main(void) {
sec = 15;
min = 13;
hour = 21;
day = 3;
date = 1;
mon = 7;
year = 25;

WDTCTL = WDTPW + WDTHOLD;


//BCSCTL1 = CALBC1_1MHZ;
//DCOCTL = CALDCO_1MHZ;
init_GPIO();
init_adc_ldr();
ngat();
I2C_USCI_init(0X27);
lcd_init();
initUART();
uart_puts("Hello world");
DS1307_init();
wire_ds1307();
//============================================
I2C_USCI_init(ADD_OLED);
OLED_init();
delay_ms(100);
OLED_clear();
delay_ms(100);

OLED_image(Launchpad_Logo);
delay_ms(20);
OLED_clear();
delay_ms(20);

__bis_SR_register(GIE);
while (1) {
doc_ldr();
trang_thai_anh_sang();
trang_thai_mua();
control();
read_ds1307();
display_sys();
hen_gio();
//=============================================================================

if(clear_display){
I2C_USCI_init(0x3C); // OLED address
OLED_clear();
clear_display = 0;
}

//================================================================================
}
}
// Phan thuc hien cac chuong trinh con
// Ham khoi tao GPIO
void init_GPIO() {

P2DIR &= ~(RAIN_SENSOR | CHE_DO | DAO_CHIEU | SW1 | SW2);

P2SEL &= ~(BTS_IN1 | BTS_IN2);


P2DIR |= (BTS_IN1 | BTS_IN2);
P2OUT &= ~(BTS_IN1 | BTS_IN2);

}
// Ham thiet lap ngat
void ngat() {
P2IE |= (CHE_DO | DAO_CHIEU); //Cho phep ngat tren cac chan CHE_DO va DAO_CHIEU
P2IES |= (CHE_DO | DAO_CHIEU); //Chon suon ngat canh xuong
P2IFG &= ~(CHE_DO | DAO_CHIEU); //Xoa co ngat
}
// Ham khoi tao ADC de doc gia tri tu cam bien anh sang
void init_adc_ldr() {

// Cau hinh ADC va chon dien tham chieu la 3.3V


ADC10CTL0 = ADC10SHT_2 + REFON + ADC10ON + SREF_0 + ADC10IE;
__delay_cycles(30);
ADC10CTL1 = INCH_4; //Chon Kenh dau vao la P1.5
__delay_cycles(30);
ADC10AE0 |= LDR_SENSOR; //Loai bo chuc nang GPIO chan P1.5
}

// Ham chuyen doi gia tri


long map(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
// Ham doc gia tri anh sang va hien thi ra lCD
void doc_ldr() {
anhsang = 100 - map(ADC10MEM, 0, 1023, 0, 100); //chuyen doi tin hieu ADC sang phan tram anh
sang
ADC10CTL0 |= ENC + ADC10SC; //cho phep bien doi ADC
__delay_cycles(1000);
if (anhsang > 99)
anhsang = 99;
}
//Ham kiem tra trang thai anh sang dua tren gia tri bien doi ADC
void trang_thai_anh_sang() {
troi = (anhsang >= 30) ? SANG : TOI;
}
//Ham kiem tra trang thai mua dua vao cam bien mua
void trang_thai_mua() {
thoitiet = (!(P2IN & RAIN_SENSOR)) ? MUA : NANG;
}
//Cac ham dieu khien dong co DC
void keo_vao() {
P2OUT &= ~BTS_IN1;
P2OUT |= BTS_IN2;
}
void keo_ra() {
P2OUT |= BTS_IN1;
P2OUT &= ~BTS_IN2;
}
void dung_dc() {
P2OUT &= ~(BTS_IN1 | BTS_IN2);
}
//==================================================
void display_status(char* time_str, char* weather_str, char* action_str) {
OLED_printText_dao(5, time_str);
OLED_printText_dao(6, weather_str);
OLED_printText_dao(7, action_str);
}

//Ham cho phep dieu khien he thong o che do tu dong/tay


void control() {
char* time_display = (troi == SANG) ? "NGAY" : " DEM";
char* weather_display = (thoitiet == MUA) ? " MUA" : "NANG";
I2C_USCI_init(0x3C);
//Khi auto_hand = 0 cho phep he thong dieu khien o che do tu dong
if (auto_hand == AUTO) {
OLED_printText(0,4,"SET:AUTO");

if ((thoitiet == MUA) && ((troi == SANG) || (troi == TOI)) && SWITCH1) {


keo_vao();
/*OLED_printText_dao(4, troi == SANG ? "NGAY" : " DEM");
OLED_printText_dao(5, " MUA");
OLED_printText_dao(6, "KEO VAO");*/
display_status(time_display, weather_display, "KEO VAO");
}
if ((thoitiet == MUA) && ((troi == SANG) || (troi == TOI)) && !SWITCH1) {
dung_dc();
/*OLED_printText_dao(4, troi == SANG ? "NGAY" : " DEM");
OLED_printText_dao(5, " MUA");
OLED_printText_dao(6, "DONG ");*/
display_status(time_display, weather_display, "DONG ");
}
if ((thoitiet == NANG) && (troi == SANG) && SWITCH2) {
keo_ra();
/*OLED_printText_dao(4, "NGAY");
OLED_printText_dao(5, "NANG");
OLED_printText_dao(6, "KEO RA ");*/
display_status(time_display, weather_display, "KEO RA ");
}
if ((thoitiet == NANG) && (troi == SANG) && !SWITCH2) {
dung_dc();
/*OLED_printText_dao(4, "NGAY");
OLED_printText_dao(5, "NANG");
OLED_printText_dao(6, "MO ");*/
display_status(time_display, weather_display, "MO ");
}
if ((thoitiet == NANG) && (troi == TOI) && SWITCH1) {
keo_vao();
/*OLED_printText_dao(4, " DEM");
OLED_printText_dao(5, "NANG");
OLED_printText_dao(6, "KEO VAO");*/
display_status(time_display, weather_display, "KEO VAO");
}
if ((thoitiet == NANG) && (troi == TOI) && !SWITCH1) {
dung_dc();
/*OLED_printText_dao(4, " DEM");
OLED_printText_dao(5, "NANG");
OLED_printText_dao(6, "DONG ");*/
display_status(time_display, weather_display, "DONG ");
}
}

//Khi auto_hand = 1 cho phep he thong dieu khien o che do nut nhan/bluetooth bang app
else {
OLED_printText(0,4,"SET:HAND");

if (vao_ra && SWITCH1) {


keo_vao();
/*OLED_printText_dao(4, troi == SANG ? "NGAY" : " DEM");
OLED_printText_dao(5, thoitiet == MUA ? " MUA" : "NANG");
OLED_printText_dao(6, "KEO VAO");*/
display_status(time_display, weather_display, "KEO VAO");
}
if (vao_ra && !SWITCH1) {
dung_dc();
/*OLED_printText_dao(4, troi == SANG ? "NGAY" : " DEM");
OLED_printText_dao(5, thoitiet == MUA ? " MUA" : "NANG");
OLED_printText_dao(6, "DONG ");*/
display_status(time_display, weather_display, "DONG ");
}
if ((vao_ra == 0) && !SWITCH2) {
dung_dc();
/*OLED_printText_dao(4, troi == SANG ? "NGAY" : " DEM");
OLED_printText_dao(5, thoitiet == MUA ? " MUA" : "NANG");
OLED_printText_dao(6, "MO ");*/
display_status(time_display, weather_display, "MO ");
}
if ((vao_ra == 0) && SWITCH2) {
keo_ra();
/*OLED_printText_dao(4, troi == SANG ? "NGAY" : " DEM");
OLED_printText_dao(5, thoitiet == MUA ? " MUA" : "NANG");
OLED_printText_dao(6, "KEO RA");*/
display_status(time_display, weather_display, "KEO RA ");
}
}
}
void send_system_status(void) {
uart_puts("CHEDO: ");
uart_puts(auto_hand ? "HAND\n" : "AUTO\n");
uart_puts("TRANG THAI: ");
uart_puts(troi == SANG ? "NGAY\n" : "DEM\n");
uart_puts("THOI TIET: ");
uart_puts(thoitiet == MUA ? "MUA" : "NANG\n");
uart_puts("GIAN PHOI: ");
uart_puts(SWITCH1 ? "DANG MO" : " DANG DONG");
}
//Ngat ADC10: Kich hoat lai CPU khi ngat ADC ket thúc
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{__bic_SR_register_on_exit(CPUOFF);}

// Ngat Port 2: xu ly khi có tin hieu tu cac nut nhan


#pragma vector=PORT2_VECTOR
__interrupt void Port_2(void) {
if (P2IFG & CHE_DO) {
auto_hand = !auto_hand;
P2IFG &= ~CHE_DO;
}
if (P2IFG & DAO_CHIEU) {
vao_ra = !vao_ra;
P2IFG &= ~DAO_CHIEU;
}
}

//Ngat nhan du lieu UART


#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
char DuLieuNhan = UCA0RXBUF;

// Xu ly cac lenh nhan tu UART


if(DuLieuNhan == '0'){
vao_ra=1;
uart_puts("DONG");
}
if(DuLieuNhan == '1'){
vao_ra =0;
uart_puts("MO");
}
if(DuLieuNhan == '2'){
auto_hand = !auto_hand;
uart_puts("MODE: ");
uart_puts(auto_hand ? "HAND" : "AUTO");
}
if(DuLieuNhan == '3'){
send_system_status();
}
if (DuLieuNhan == '\r' || DuLieuNhan == '\n') {
chuoi[uart_index] = '\0';
uart_index = 0;
chuoi_ready = 1; //dánh d?u chu?i s?n sàng
}
else {
if (uart_index < sizeof(chuoi) - 1) {
chuoi[uart_index++] = DuLieuNhan;
}
}
if(DuLieuNhan == 'H'){
clear_display = 1;
}
}

You might also like