Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sbit SoftSpi_CLK at RA0_bit;
- sbit SoftSpi_SDI at RA1_bit;
- sbit SoftSpi_SDO at RA2_bit;
- sbit SoftSpi_CLK_Direction at TRISA0_bit;
- sbit SoftSpi_SDI_Direction at TRISA1_bit;
- sbit SoftSpi_SDO_Direction at TRISA2_bit;
- sbit LCD_RS at RB4_bit;
- sbit LCD_EN at RB5_bit;
- sbit LCD_D4 at RB0_bit;
- sbit LCD_D5 at RB1_bit;
- sbit LCD_D6 at RB2_bit;
- sbit LCD_D7 at RB3_bit;
- sbit LCD_RS_Direction at TRISB4_bit;
- sbit LCD_EN_Direction at TRISB5_bit;
- sbit LCD_D4_Direction at TRISB0_bit;
- sbit LCD_D5_Direction at TRISB1_bit;
- sbit LCD_D6_Direction at TRISB2_bit;
- sbit LCD_D7_Direction at TRISB3_bit;
- unsigned short value;
- unsigned short read;
- char uart_rd;
- char broj[10];
- char niza[16];
- int counter;
- int i;
- short shouldBreak = 0;
- short oneDollar = 0;
- int temp;
- int zbir;
- void printOnScreen(){
- Lcd_Cmd(_LCD_CLEAR);
- temp = 16 - counter;
- for (i = 1; i <= temp;i++){
- Lcd_Chr(1,i,' ');
- }
- for(i = temp + 1; i <= 16; i++){
- Lcd_Chr(i,i,niza[i - temp - 1]);
- }
- Delay_ms(1);
- }
- void sendBySPI(){
- char* zbirot;
- IntToStr(zbir,broj);
- zbirot = Ltrim(broj);
- Lcd_Cmd(_LCD_CLEAR);
- Lcd_Out(1,1,"Prakjam zbir");
- Lcd_Out(2,1,zbirot);
- Delay_ms(5);
- if (zbir > 10){
- if (zbir > 99){
- Soft_SPI_Write(broj[7]);
- Soft_SPI_Write(broj[8]);
- Soft_SPI_Write(broj[9]);
- }
- else{
- Soft_SPI_Write(broj[8]);
- Soft_SPI_Write(broj[9]);
- }
- }else{
- Soft_SPI_Write(broj[9]);
- }
- }
- void printError(){
- Lcd_Cmd(_LCD_CLEAR);
- Lcd_Out(1,1,Ltrim("Error!"));
- Delay_ms(1);
- }
- void printEnd(){
- Lcd_Cmd(_LCD_CLEAR);
- Lcd_Out(1,1,Ltrim("End"));
- }
- void main() {
- ANSEL = 0;
- ANSELH = 0;
- C1ON_bit = 0;
- C2ON_bit = 0;
- Soft_SPI_Init();
- UART1_Init(9600);
- Lcd_Init();
- Lcd_Cmd(_LCD_CURSOR_OFF);
- Lcd_Cmd(_LCD_CLEAR);
- Delay_ms(5);
- counter = 0;
- zbir = 0;
- while(1){
- if (UART1_Data_Ready()) {
- uart_rd = UART1_Read();
- Lcd_Cmd(_LCD_CLEAR);
- Lcd_Chr(1,1,uart_rd);
- Delay_ms(1);
- if (uart_rd >= '0' && uart_rd <= '9'){
- if (counter == 16){
- printError();
- oneDollar = 0;
- counter = 0;
- for (i = 0; i < 16; i++){
- niza[i] = ' ';
- }
- }else{
- niza[counter] = uart_rd;
- zbir = zbir + (uart_rd - '0');
- counter = counter + 1;
- intToStr(counter,broj);
- Lcd_Out(2,1,"C: ");
- Lcd_Out(2,4,broj);
- delay_ms(1);
- oneDollar = 0;
- }
- }else if (uart_rd == '$'){
- if (oneDollar){
- printEnd();
- shouldBreak = 1;
- }else{
- oneDollar = 1;
- printOnScreen();
- sendBySPI();
- counter = 0;
- zbir = 0;
- for (i = 0; i < 16; i++){
- niza[i] = ' ';
- }
- }
- }else{
- printError();
- oneDollar = 0;
- counter = 0;
- zbir = 0;
- for (i = 0; i < 16; i++){
- niza[i] = ' ';
- }
- }
- }
- if (shouldBreak){
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement