Code: Select all
//#############################################################################
//Device MCU:PIC18F46K22
//OSC:20MHz
//Mustafa ERDGAN mustafa-erdogan@outlook.com
//MicroC Version 7.0.1 and Proteus 8.9 Sp2
//#############################################################################
//-------- GCLD BAGLANTILARI-------------
char GLCD_DataPort at PORTB;
sbit GLCD_CS1 at RD1_bit;
sbit GLCD_CS2 at RD0_bit;
sbit GLCD_RS at RD2_bit;
sbit GLCD_RW at RD3_bit;
sbit GLCD_EN at RD4_bit;
sbit GLCD_RST at RD5_bit;
sbit GLCD_CS1_Direction at TRISD1_bit;
sbit GLCD_CS2_Direction at TRISD0_bit;
sbit GLCD_RS_Direction at TRISD2_bit;
sbit GLCD_RW_Direction at TRISD3_bit;
sbit GLCD_EN_Direction at TRISD4_bit;
sbit GLCD_RST_Direction at TRISD5_bit;
//TANIMLAMALAR------------
#define Sensor1 PORTA.RA0
#define Sensor2 PORTA.RA1
#define Buzzer PORTC.RC0
#define Motor11 PORTA.RA2
#define Motor12 PORTA.RA3
#define Motor21 PORTA.RA4
#define Motor22 PORTA.RA5
//DEGISKENLER------------
unsigned char uart_st;
unsigned char old_uart_st;
unsigned char uart_rd;
unsigned char old_uart_rd;
void send_text() // OUT-----------
{
old_uart_st=uart_st;
if (Sensor1==1)
{
uart_st='A';
}
else if(Sensor2==1)
{
uart_st='B';
}
else
{
uart_st='N';
}
//sending part
if (UART1_Tx_Idle() == 1) {
UART1_Write(uart_st);
}
//showing part
if(old_uart_st!=uart_st)
{
Glcd_Box(0,0,6,8,0); // for clear
Glcd_Write_Char_Adv(uart_st,0,0);
}
}
void system_control()
{
//N-symbol for epmty time
//A-symbol for Motor11=1;
//B-symbol for Motor12=1;
//K-symbol for Uart Disconnected
//showing part // this is not working very well because of receving part
// not working very well
if(old_uart_rd!=uart_rd)
{
Glcd_Box(120,0,126,8,0); // for clear
Glcd_Write_Char_Adv(uart_rd,120,0);
}
if(uart_rd=='A')
{
Motor11=1;
}
else
{
Motor11=0;
}
if(uart_rd=='B')
{
Motor12=1;
}
else
{
Motor12=0;
}
}
void read_text()
{
old_uart_rd=uart_rd;
if (UART1_Data_Ready())
{// If data is received,
// A character is available.
if(RCSTA1.OERR==1)
{
// Overrun error.
// Clear the error and re-enable the receiver.
RCSTA1.CREN=0;
RCSTA1.CREN=1;
}
if(RCSTA1.FERR==1) // STEP-9 CHECK FRAME ERROR
{
// Framing error.
// Read the byte to clear the error but don't use the byte.
uart_rd = UART1_Read(); // read the received data,
}
else
{
// A byte was received without error.
uart_rd = UART1_Read(); // read the received data,
// Do something with the byte.
system_control();
}
}
else
{
uart_rd ='K';
system_control();
}
}
void micro_setup()
{
// ANALOG PORT IPTAL
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
ADCON0 = 0;
ADRESH=0;
ADRESL=0;
ANSELA=0;
ANSELB=0;
ANSELC=0;
ANSELD=0;
ANSELE=0;
// PORT KURULUMLARI
TRISA=0b11000011;
TRISB=0b00000000;
TRISC=0b11000000;
TRISD=0;
TRISE=0b00001111;
PORTA=0;
PORTB=0;
PORTC=0;
PORTD=0;
PORTE=0;
// GLCD EKRAN KURUMU
Glcd_Init(); // GLCD AÇILIYOR
Glcd_Fill(0x00); // GLCD TEMIZLE
// UART KURULUM
PMD0.UART1MD=0;
UART1_Init(115200);
Delay_ms(100);
UART1_Write('S');
}
void main() {
micro_setup();
while(1)
{
send_text();
read_text();
system_control();
}
}
Code: Select all
//#############################################################################
//Device MCU:PIC18F25K22
//OSC:20MHz
//Mustafa ERDGAN mustafa-erdogan@outlook.com
//MicroC Version 7.0.1
//#############################################################################
//TANIMLAMALAR
#define Sensor1 PORTA.RA3
#define Sensor2 PORTC.RC0
#define Limit1 PORTA.RA0
#define Limit2 PORTA.RA1
#define LimitEMC PORTA.RA2
#define Buzzer PORTA.RA4
#define Motor11 PORTC.RC1
#define Motor12 PORTC.RC2
#define Motor21 PORTC.RC4
#define Motor22 PORTC.RC5
sbit LCD_RS at LATB0_bit;
sbit LCD_EN at LATB1_bit;
sbit LCD_D4 at LATB2_bit;
sbit LCD_D5 at LATB3_bit;
sbit LCD_D6 at LATB4_bit;
sbit LCD_D7 at LATB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
unsigned char uart_rd;
void system_control()
{
//N-symbol for epmty time
//A-symbol for Motor11=1;
//B-symbol for Motor12=1;
//K-symbol for Uart Disconnected
Lcd_Chr(2,5,uart_rd);
if(uart_rd=='A')
{
Motor11=1;
}
else
{
Motor11=0;
}
if(uart_rd=='B')
{
Motor12=1;
}
else
{
Motor12=0;
}
}
void send_text()
{
if (Sensor1==1)
{
UART1_Write('A');
Lcd_Chr(1,5,'A');
}
else if(Sensor2==1)
{
UART1_Write('B');
Lcd_Chr(1,5,'B');
}
else
{
UART1_Write('N');
Lcd_Chr(1,5,'N');
}
}
void read_text()
{
if (UART1_Data_Ready())
{// If data is received,
// A character is available.
if(RCSTA1.OERR==1)
{
// Overrun error.
// Clear the error and re-enable the receiver.
RCSTA1.CREN=0;
RCSTA1.CREN=1;
}
if(RCSTA1.FERR==1) // STEP-9 CHECK FRAME ERROR
{
// Framing error.
// Read the byte to clear the error but don't use the byte.
uart_rd = UART1_Read(); // read the received data,
}
else
{
// A byte was received without error.
uart_rd = UART1_Read(); // read the received data,
// Do something with the byte.
system_control();
}
}
else
{
uart_rd ='K';
system_control();
}
}
void micro_setup()
{
// ANALOG PORT IPTAL
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
ADCON0 = 0;
ANSELA=0;
ANSELB=0;
ANSELC=0;
// PORT KURULUMLARI
TRISA=0b00001111;
TRISB=0b00000000;
TRISC=0b11000001;
PORTA=0;
PORTB=0;
PORTC=0;
// LCD EKRAN KURUMU
Lcd_Init();
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Cmd(_LCD_CLEAR); // Clear display
// UART KURULUM
PMD0.UART1MD=0;
UART1_Init(115200);
Delay_ms(100);
UART1_Write('S');
}
void main() {
micro_setup();
Lcd_Out(1,1,"OUT:");
Lcd_Out(2,2,"IN:");
while(1)
{
read_text();
send_text();
system_control();
// for following code flow.Not necassary.
if(Sensor1==1)
{
Lcd_Out(3,3,"<");
}
else
{
Lcd_Out(3,3,">");
}
}
}