/********************
* main.c *
********************//
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/examples/i2c_master_example.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*=== USB library ===*/
#define BFSIZE 32
char NEWLINE[] = "\r\n";
static char rBuf[BFSIZE]; // USB Read Buffer
static char wBuf[BFSIZE]; // USB Write Buffer
int tmFlg; //?Timer2?Flag
void SendUSB_char(char c){
do
CDCTxService();
while (!USBUSARTIsTxTrfReady());
putUSBUSART((char *)&c, 1);
CDCTxService();
}
void SendUSB_str(char *str){
do
CDCTxService();
while (!USBUSARTIsTxTrfReady());
putsUSBUSART((char *)str);
CDCTxService();
}
void SendUSB_int(int val){
unsigned char iBuf[10];
sprintf(iBuf,"%d: ",val);
SendUSB_str(iBuf);
}
/*==== LCD library ===*/
#define I2CLCD_AQM0802A 0x3e
#define CONTRAST 0x18 // for 5.0V
//#define CONTRAST 0x30 // for 3.3V
#define BOOST 0x00 // for 5.0V Bon=off
//#define BOOST 0x04 // for 3.3V Bon=on
void LCD_dat(char chr){
I2C_Write1ByteRegister(I2CLCD_AQM0802A, 0x40, chr);
__delay_us(30); // 30us
}
//-------- send command -------------------------
void LCD_cmd(char cmd){
I2C_Write1ByteRegister(I2CLCD_AQM0802A, 0x00, cmd);
if(cmd & 0xFC) // bit6 = 1
__delay_us(30); // 30us
else
__delay_ms(2); // 2ms Clear or Home
}
//-------- clear LCD--------------------------
void LCD_clr(void){
LCD_cmd(0x01);
}
//--------- Home -----------------------------
void LCD_home(void){
LCD_cmd(0x02);
}
//--------- Cursor X,Y -----------------------
void LCD_cursor(int x, int y){
if (y == 0)
LCD_cmd(0x80 + x);
else if (y == 1)
LCD_cmd(0xc0 + x);
}
//-------- display strings -------------------------
void LCD_str(char *str){
while(*str)
LCD_dat(*str++); //pointer increment
}
//-------- write 1 character to LCD ----------------
void putch(char ch){
LCD_dat(ch);
}
//-------- LCD initialize ---------------------------
void LCD_init(){
__delay_ms(40); // 40ms wait
LCD_cmd(0x38); // 8bit,2line
LCD_cmd(0x39); // IS=1 : extention mode set
LCD_cmd(0x14); // Internal OSC Frequency
LCD_cmd(0x70 + (CONTRAST & 0x0F)); // Contrast set
LCD_cmd(0x58 + BOOST + (CONTRAST >> 4));
// Power/ICON/Contrast Control
LCD_cmd(0x6C); // Follower control
__delay_ms(200); // 200ms wait
LCD_cmd(0x38); // IS=0 : extention mode cancel
LCD_cmd(0x0C); // Display ON
LCD_cmd(0x01); // Clear Display
__delay_ms(2); // wait more than 1.08ms
}
/*==== Main application ==== */
void main(void)
{
SYSTEM_Initialize(); // initialize device
INTERRUPT_GlobalInterruptEnable(); // Enable Global
INTERRUPT_PeripheralInterruptEnable(); // Enable Peripheral
int swFlg = 0; // Switch Flag
int adFlg = 0; // ADconvert Flag
int num, i;
unsigned short aVal; // AD Converted data
LCD_init();
char msg[] = "Hello!";
LCD_str(msg);
while (1) {
num = getsUSBUSART(rBuf,sizeof(rBuf));
if(num > 0) {
for(i = 0; i < num; i++) {
switch (rBuf[i]) {
case 0x0A:
case 0x0D:
wBuf[i] = 0x00;
break;
default:
wBuf[i] = rBuf[i];
break;
}
}
LCD_clr();
LCD_str(wBuf);
SendUSB_str("PIC> ");
SendUSB_str(wBuf);
SendUSB_str(NEWLINE);
if(( wBuf[0] & 0x01) == 0)
LED_SetLow();
else
LED_SetHigh();
}
if((SWC3_GetValue() == 0) & (swFlg == 0)){
swFlg = 1;
if(adFlg == 0){
SendUSB_str("ADC Start");
SendUSB_str(NEWLINE);
adFlg = 1;
}else{
SendUSB_str("ADC Hold");
SendUSB_str(NEWLINE);
adFlg = 0;
}
}else if((SWC3_GetValue() > 0)& (swFlg == 1)){
wFlg = 0;
}
aVal = ADC_GetConversion(3);
if((tmFlg > 0) & (adFlg > 0)){
sprintf(wBuf,"ADC:%4d\n\r",aVal);
LCD_cursor(0,1);
LCD_str(wBuf);
SendUSB_str(wBuf);
SendUSB_str(NEWLINE);
tmFlg = 0;
}
}
}
|