Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. /*
  2. * File: main.c
  3. * Author: Bennett and Bec
  4. *
  5. * Created on 15 October 2019, 14:59
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "ConfigRegsPIC18f4520.h"
  11. #include <timers.h>
  12. #include <math.h>
  13. #include <string.h>
  14. #include <p18cxxx.h>
  15.  
  16. #include "motor.h"
  17.  
  18. #define M_PI 3.14159265358979323846264338327950288
  19.  
  20. // Function declarations
  21. void configurePORTB(void);
  22. void configureISR(void);
  23. void lowPriorityISR (void);
  24. void low_interrupt(void);
  25. float getLMotorSpeed(void);
  26. void encoderISR(void);
  27.  
  28. void configureSerial(void);
  29. int tx232C(unsigned char * txPtr);
  30.  
  31. // Global variables
  32. float encoder_left_t1;
  33. float encoder_left_t2;
  34. int encoder_overflow_counter;
  35. float encoder_time_diff;
  36. char str[] = "hello";
  37. char buffer[10];
  38.  
  39. const float wheel_radius = 0.045;
  40. const int counts_per_rev = 32;
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. //Define the low priority interrupt at 0x0018
  49. //start the service routine for low priority interrupt
  50. #pragma interruptlow lowPriorityISR
  51. void lowPriorityISR (void){
  52. encoderISR();
  53. }
  54.  
  55. #pragma code lowPriorityInterruptAddress=0x0018
  56. void low_interrupt(void){
  57. //All that needs to happen in the interrupt is to goto the service routine
  58. _asm
  59. GOTO lowPriorityISR
  60. _endasm
  61. }
  62.  
  63. #pragma code //default code section
  64.  
  65.  
  66.  
  67. void main(void) {
  68.  
  69. //Go to the configure function
  70.  
  71. configurePORTB();
  72. configureSerial();
  73. configureISR();
  74.  
  75. // init
  76. initMotors();
  77.  
  78. // Set individually
  79. setMotorL(20);
  80. setMotorR(20);
  81.  
  82.  
  83.  
  84.  
  85. while(1)
  86. {
  87. //tx232C(str);
  88.  
  89.  
  90. itoa(getLMotorSpeed(), buffer);
  91. tx232C(buffer);
  92.  
  93. }
  94.  
  95. }
  96.  
  97. void configureISR(void)
  98. {
  99. RCONbits.IPEN = 1; // enable priority interrupts
  100.  
  101.  
  102. INTCONbits.RBIE = 1; // enables interrupt on PORTB change
  103. INTCON2bits.RBIP = 0; // low priority interrupt
  104.  
  105. //INTCON2bits.INTEDG0 = 1; // interrupt on rising edge (RB0)
  106. //INTCON2bits.INTEDG1 = 1;
  107.  
  108. T1CON = 0b10000001; // enable timer 1, prescaler of 1, 16 bit resolution
  109.  
  110. PIE1bits.TMR1IE = 1; // enable timer 1 overflow interrupt
  111.  
  112. INTCONbits.GIEH = 1; //Enable high priority interrupts
  113. INTCONbits.GIEL = 1; //Enable low priority interrupts
  114.  
  115. }
  116.  
  117. void configurePORTB(void)
  118. {
  119. TRISB = 1; // set PORTB to input (***change to bits 4-5***)
  120. PORTB = 0; // clear PORTB
  121. }
  122.  
  123.  
  124. void encoderISR(void)
  125. {
  126. if(PIR1bits.TMR1IF == 1)
  127. {
  128. encoder_overflow_counter++; // increment counter
  129. //if(encoder_overflow_counter)
  130. PIR1bits.TMR1IF = 0; // clear overflow bit flag
  131. }
  132.  
  133. if(INTCONbits.RBIF == 1)
  134. {
  135. // find time difference
  136. encoder_left_t2 = ReadTimer1();
  137. encoder_time_diff = encoder_overflow_counter*(2^16) + encoder_left_t2 - encoder_left_t1;
  138. encoder_left_t1 = encoder_left_t2;
  139. encoder_overflow_counter = 0;
  140.  
  141. INTCONbits.RBIF = 0; // clear flag
  142. }
  143.  
  144. }
  145.  
  146.  
  147. float getLMotorSpeed(void)
  148. {
  149. return wheel_radius*(2*M_PI)/(counts_per_rev*encoder_time_diff);
  150. }
  151.  
  152.  
  153. void configureSerial(void){
  154. //High speed baud rate setting (baud = 9600)
  155. SPBRG = 15;
  156. //Configure for transmission
  157. TXSTA = 0b00100000;
  158. RCSTA = 0b10010000;
  159.  
  160. }
  161.  
  162. int tx232C(unsigned char * txPtr){
  163. //Check for null terminating character, stay in loop while not null
  164. while(*txPtr != '\0')
  165. {
  166. if(TXSTAbits.TRMT == 1)
  167. {
  168. TXREG = *txPtr;
  169. txPtr++;
  170. }
  171. }
  172. //When null is reached, transmit the newline and return characters
  173. while(1){
  174. if(TXSTAbits.TRMT == 1){
  175. TXREG = '\n';
  176. while(1){
  177. if(TXSTAbits.TRMT == 1){
  178. TXREG = '\r';
  179. return 1;
  180. }
  181. }
  182. }
  183. }
  184. }
  185.  
  186.  
  187. // ----------------------------------------------------------------------------
  188.  
  189.  
  190.  
  191.  
  192.  
  193. // Initialise the struct for the motors
  194. motor motorL, motorR;
  195.  
  196. // Initialise the motors and relevant PWM shenanigans
  197. void initMotors(void) {
  198. // Make the PWM and direction pins output
  199. MOTL_PWM_TRIS = 0;
  200. MOTR_PWM_TRIS = 0;
  201. MOTL_DIR_TRIS = 0;
  202. MOTR_DIR_TRIS = 0;
  203.  
  204. MOTL_CCPCON = 0b00001100; // PWM mode
  205. MOTR_CCPCON = 0b00001100; // PWM mode
  206. // Maximum period (gives a 9.8kHz signal)
  207. // T = 1/(10*10^6) * 4 * 256 = 102.4us or 9.766kHz
  208. MOT_PER_REG = 0xFF;
  209.  
  210. //T2CON = 0b00000100; // T2 on
  211.  
  212. // Enable timer 2
  213. T2CONbits.TMR2ON = 1;
  214.  
  215. }
  216.  
  217. // Sets the speed of both motors with one function
  218. void setMotors(char speedL, char speedR) {
  219. setMotorL(speedL);
  220. setMotorR(speedR);
  221. }
  222.  
  223. // Sets the speed/duty cycle and direction of the left motor
  224. // Speed is from -127 to 127
  225. void setMotorL(char speed) {
  226. //if ((speed > SPEED_ABS_MAX) || (speed < SPEED_ABS_MIN)) return;
  227. motorL.speed = speed;
  228. if(speed < 0) {
  229. speed *= -1;
  230. MOTL_DIR = BACKWARD;
  231. } else {
  232. MOTL_DIR = FORWARD;
  233. }
  234. MOTL_DUTY = speed * PWM_SCALE;
  235.  
  236. }
  237.  
  238. // Sets the speed/duty cycle and direction of the right motor
  239. // Speed is from -127 to 127
  240. void setMotorR(char speed) {
  241. //if ((speed > SPEED_ABS_MAX) || (speed < SPEED_ABS_MIN)) return;
  242. motorR.speed = speed;
  243. if(speed < 0) {
  244. speed *= -1;
  245. MOTR_DIR = BACKWARD;
  246. } else {
  247. MOTR_DIR = FORWARD;
  248. }
  249. MOTR_DUTY = speed * PWM_SCALE;
  250.  
  251. }
  252.  
  253. //
  254. char getMotSpeedL(void) {
  255. return motorL.speed;
  256. }
  257.  
  258. char getMotSpeedR(void) {
  259. return motorR.speed;
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement