Inital working code
This commit is contained in:
		
							
								
								
									
										224
									
								
								main.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										224
									
								
								main.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,224 @@ | ||||
|  /* | ||||
|  * MAIN Generated Driver File | ||||
|  *  | ||||
|  * @file main.c | ||||
|  *  | ||||
|  * @defgroup main MAIN | ||||
|  *  | ||||
|  * @brief This is the generated driver implementation file for the MAIN driver. | ||||
|  * | ||||
|  * @version MAIN Driver Version 1.0.2 | ||||
|  * | ||||
|  * @version Package Version: 3.1.2 | ||||
| */ | ||||
|  | ||||
| /* | ||||
| <EFBFBD> [2025] Microchip Technology Inc. and its subsidiaries. | ||||
|  | ||||
|     Subject to your compliance with these terms, you may use Microchip  | ||||
|     software and any derivatives exclusively with Microchip products.  | ||||
|     You are responsible for complying with 3rd party license terms   | ||||
|     applicable to your use of 3rd party software (including open source   | ||||
|     software) that may accompany Microchip software. SOFTWARE IS ?AS IS.?  | ||||
|     NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS  | ||||
|     SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT,   | ||||
|     MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT  | ||||
|     WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,  | ||||
|     INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY  | ||||
|     KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF  | ||||
|     MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE  | ||||
|     FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP?S  | ||||
|     TOTAL LIABILITY ON ALL CLAIMS RELATED TO THE SOFTWARE WILL NOT  | ||||
|     EXCEED AMOUNT OF FEES, IF ANY, YOU PAID DIRECTLY TO MICROCHIP FOR  | ||||
|     THIS SOFTWARE. | ||||
| */ | ||||
| #include "mcc_generated_files/system/system.h" | ||||
| #include <stdlib.h> | ||||
| #include "commons.h" | ||||
| #include "proto.h" | ||||
|  | ||||
| static uint16_t   led1, led2, sensor; | ||||
| static errors_t   peer_err = SUCCESS, retcode = SUCCESS; | ||||
| static uint8_t    in_rx, cmd, peer_id = 0; | ||||
| static bool       rx_over = false, update_sensor = false; | ||||
| volatile bool tx_timed_out = false; | ||||
|  | ||||
| //============================================================================== | ||||
| // Private Declarations | ||||
| //============================================================================== | ||||
| static void init(); | ||||
| static void show_error(); | ||||
|  | ||||
| static void tmr_tx_cb(); | ||||
| static void tmr_sensor_cb(); | ||||
|  | ||||
| //============================================================================== | ||||
| // Public | ||||
| //============================================================================== | ||||
| /*=****************************************************************************/ | ||||
| int main(void) | ||||
| { | ||||
|     init(); | ||||
|      | ||||
| //    uint16_t i = 0; | ||||
| //    int change = 1; | ||||
| //     | ||||
| //    while(1) | ||||
| //    { | ||||
| ////        PWM3_LoadDutyValue(i); | ||||
| ////        PWM4_LoadDutyValue(i); | ||||
| ////        __delay_ms(1); | ||||
| ////         | ||||
| ////        if (i == 1024) | ||||
| ////            change = -1; | ||||
| ////        else if (i == 0) | ||||
| ////            change = 1; | ||||
| ////         | ||||
| ////        i += change; | ||||
| //         | ||||
| //        // Update sensor if it's time | ||||
| //        if (update_sensor) | ||||
| //        { | ||||
| //            update_sensor = false; | ||||
| //            ADC_StartConversion(); | ||||
| //            while(!ADC_IsConversionDone()); | ||||
| //            sensor = ADC_GetConversionResult(); | ||||
| //        } | ||||
| //         | ||||
| //        retcode = uart_rx_byte(&in_rx); | ||||
| //        if (retcode == SUCCESS) | ||||
| //        { | ||||
| //            rx_over = byte_in(in_rx, &cmd, &peer_id, &led1, &led2, &sensor, &peer_err); | ||||
| //            if (rx_over) | ||||
| //            { | ||||
| //                switch (cmd) | ||||
| //                { | ||||
| //                  case GET_CMD_ID: | ||||
| //                      // Wait for Master to stop writing | ||||
| //                      __delay_ms(DELAY_TO_REPLY); | ||||
| //                      retcode = send_data(MY_ID, sensor); | ||||
| //                    break; | ||||
| //                  case SET_CMD_ID: | ||||
| //                    PWM3_LoadDutyValue(led1); | ||||
| //                    PWM4_LoadDutyValue(led2); | ||||
| //                    __delay_ms(DELAY_TO_REPLY); | ||||
| //                    retcode = send_error(MY_ID, SUCCESS); | ||||
| //                  break; | ||||
| //                  default: | ||||
| //                      DEBUG_Toggle(); | ||||
| //                    retcode = UNSUPPORTED_CMD; | ||||
| //                    break; | ||||
| //                } | ||||
| //            } | ||||
| //        } | ||||
| //    } | ||||
| //     | ||||
|     while(1) | ||||
|     { | ||||
|         if (RET_FAILURE(retcode)) | ||||
|             show_error(); | ||||
|          | ||||
|         // Update sensor if it's time | ||||
|         if (update_sensor) | ||||
|         { | ||||
|             update_sensor = false; | ||||
|             ADC_StartConversion(); | ||||
|             while(!ADC_IsConversionDone()); | ||||
|             sensor = ADC_GetConversionResult(); | ||||
|         } | ||||
|          | ||||
|         retcode = uart_rx_byte(&in_rx); | ||||
|         if (retcode == SUCCESS) | ||||
|         { | ||||
|             rx_over = byte_in(in_rx, &cmd, &peer_id, &led1, &led2, &sensor, &peer_err); | ||||
|             if (rx_over) | ||||
|             { | ||||
|                 switch (cmd) | ||||
|                 { | ||||
| #ifndef IS_MASTER | ||||
|                   case GET_CMD_ID: | ||||
|                       // Wait for Master to stop writing | ||||
|                       __delay_ms(DELAY_TO_REPLY); | ||||
|                       retcode = send_data(MY_ID, sensor); | ||||
|                     break; | ||||
|                      | ||||
|                   case SET_CMD_ID: | ||||
|                       PWM3_LoadDutyValue(led1); | ||||
|                       PWM4_LoadDutyValue(led2); | ||||
|                       __delay_ms(DELAY_TO_REPLY); | ||||
|                       retcode = send_error(MY_ID, SUCCESS); | ||||
|                     break; | ||||
| #else | ||||
|                   case DATA_CMD_ID: | ||||
|                     Serial.print("Sensor: "); | ||||
|                     Serial.println(sensor); | ||||
|                     break; | ||||
|  | ||||
|                   case ERROR_CMD_ID: | ||||
|                     Serial.print("Peer error: "); | ||||
|                     Serial.println(peer_err); | ||||
|                     break; | ||||
| #endif | ||||
|                   default: | ||||
|                     retcode = UNSUPPORTED_CMD; | ||||
|                     break; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| //============================================================================== | ||||
| // Private | ||||
| //============================================================================== | ||||
| /*=****************************************************************************/ | ||||
| static void init() | ||||
| { | ||||
|     SYSTEM_Initialize(); | ||||
|     INTERRUPT_GlobalInterruptEnable();  | ||||
|     INTERRUPT_PeripheralInterruptEnable();  | ||||
|      | ||||
|     ADC_Initialize(); | ||||
|     ADC_SelectChannel(channel_ANC0); | ||||
|  | ||||
|     // TMR callback | ||||
|     TMR_TX_OverflowCallbackRegister(tmr_tx_cb); | ||||
|     TMR_SENSOR_OverflowCallbackRegister(tmr_sensor_cb); | ||||
|      | ||||
|     // Initial variables and pins | ||||
|     RS_MODE_SetLow(); | ||||
|     TMR_TX_Stop(); | ||||
|     tx_timed_out = false; | ||||
|      | ||||
|     DEBUG_SetHigh(); | ||||
|     __delay_ms(1000); | ||||
|     DEBUG_SetLow(); | ||||
| } | ||||
|  | ||||
| /*=****************************************************************************/ | ||||
| static void show_error() | ||||
| { | ||||
|     uint16_t time = retcode * 100; | ||||
|      | ||||
|     while(1) | ||||
|     { | ||||
|         DEBUG_Toggle(); | ||||
|          | ||||
|         for (uint16_t i = 0; i < time; ++i) | ||||
|             __delay_ms(1); | ||||
|     } | ||||
| } | ||||
|  | ||||
| /*=****************************************************************************/ | ||||
| static void tmr_tx_cb() | ||||
| { | ||||
|     tx_timed_out = true; | ||||
| } | ||||
|  | ||||
| /*=****************************************************************************/ | ||||
| static void tmr_sensor_cb() | ||||
| { | ||||
|     update_sensor = true; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user