PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/car_stuff/car_1/main.c

https://bitbucket.org/dukzcry/hobbies
C | 442 lines | 125 code | 78 blank | 239 comment | 21 complexity | 7101c76ac8afe214d1df79c42ed06ee7 MD5 | raw file
  1. // http://compcar.ru
  2. /* Includes ------------------------------------------------------------------*/
  3. #include "stm32f10x.h"
  4. #include <stdio.h>
  5. // #include <string.h>
  6. #include <math.h>
  7. #include "stm32_pereph_init.h"
  8. #include "main.h"
  9. /* Private typedef -----------------------------------------------------------*/
  10. /* Private define ------------------------------------------------------------*/
  11. #define AV_PIN GPIO_Pin_3
  12. #define REV_PIN GPIO_Pin_4
  13. #define USER_PIN GPIO_Pin_0
  14. #define BLUE_PIN GPIO_Pin_8
  15. #define CAM_ON_WAIT 500
  16. #define CAM_OFF_WAIT 10
  17. /* Private macro -------------------------------------------------------------*/
  18. /* Private variables ---------------------------------------------------------*/
  19. extern uint16_t AdcOutArray[ADC_BUFFER];
  20. uint32_t Light = 0;
  21. uint32_t Brightness = 1;
  22. uint32_t BrightnessPrev = 1;
  23. uint32_t SysClk;
  24. uint16_t ReverseOnCnt;
  25. uint16_t ReverseOffCnt;
  26. uint16_t ButtonDownCnt;
  27. uint8_t PWM_Prescaler = 0;
  28. uint32_t BrightnessVsLight[2][20] = {
  29. {0, 100, 170, 215, 225, 230},
  30. {1, 10, 30, 90, 200, 255}
  31. };
  32. uint8_t K = 1; // ??????????? ???????? ?? ???????? ?????????? ???????
  33. /* Private function prototypes -----------------------------------------------*/
  34. void WaitMs (uint16_t Ms);
  35. uint32_t TableLookup1d(uint32_t Inp, uint32_t In[][20], uint32_t ArrSize);
  36. /* Private functions ---------------------------------------------------------*/
  37. int main (void)
  38. {
  39. for (int i = 0; i < 20; i++)
  40. {
  41. BrightnessVsLight[0][i] <<= 4;
  42. }
  43. for (int i = 0; i < 20; i++)
  44. {
  45. BrightnessVsLight[1][i] <<= 8;
  46. }
  47. Peripherals_Hardware_Startup();
  48. while (1)
  49. {
  50. SysClk++;
  51. /* ?????????????? ??????? */
  52. for (int i = 0; i < ADC_BUFFER; i++)
  53. {
  54. Light += (uint32_t)AdcOutArray[i];
  55. }
  56. Light /= ADC_BUFFER; // ??????? ???????? ????????????
  57. BrightnessPrev = Brightness; // ??????? ???????? ???????
  58. Brightness = TableLookup1d(Light, BrightnessVsLight, 6); // ???????? ???????? ???????
  59. if (Brightness > BrightnessPrev) // ?????? ????
  60. {
  61. Brightness = BrightnessPrev + K * (Brightness - BrightnessPrev) / 1000;
  62. }
  63. else if (Brightness < BrightnessPrev) // ?????? ??????
  64. {
  65. Brightness = BrightnessPrev - K * (BrightnessPrev - Brightness) / 1000;
  66. }
  67. TIM_SetCompare3(TIM2, Brightness);
  68. /* ???????? ?????? ??????? ???? */
  69. if (GPIO_ReadInputDataBit(GPIOA, REV_PIN)) // ?????? ???????? ????????
  70. {
  71. ReverseOffCnt = 0;
  72. if (!GPIO_ReadOutputDataBit(GPIOC, AV_PIN)) // ?????? ?? ????????
  73. {
  74. ReverseOnCnt++;
  75. if (ReverseOnCnt > CAM_ON_WAIT) // ????????? 500 ??
  76. {
  77. GPIO_SetBits(GPIOC, AV_PIN | BLUE_PIN); // ???????? ??????
  78. ReverseOnCnt = 0;
  79. }
  80. }
  81. }
  82. else // ?????? ???????? ?????????
  83. {
  84. ReverseOnCnt = 0;
  85. if (GPIO_ReadOutputDataBit(GPIOC, AV_PIN)) // ?????? ????????
  86. {
  87. ReverseOffCnt++;
  88. if (ReverseOffCnt > CAM_OFF_WAIT) // ????????? 5000 ??
  89. {
  90. GPIO_ResetBits(GPIOC, AV_PIN | BLUE_PIN); // ???????? ??????
  91. ReverseOffCnt = 0;
  92. }
  93. }
  94. }
  95. /* ????????? ?????? USER1 */
  96. // if (GPIO_ReadInputDataBit(GPIOA, USER_PIN)) // ?????? ??????
  97. // {
  98. // ButtonDownCnt++; // ????????
  99. // if (ButtonDownCnt > 500) // ?????????
  100. // {
  101. // PWM_Prescaler++;
  102. // if (PWM_Prescaler > 3) PWM_Prescaler = 0;
  103. // TIM_PrescalerConfig(TIM2, PWM_Prescaler, TIM_PSCReloadMode_Update);
  104. // ButtonDownCnt = 0;
  105. // }
  106. //
  107. //
  108. // }
  109. // else
  110. // {
  111. // ButtonDownCnt = 0;
  112. // }
  113. WaitMs(1);
  114. GPIO_WriteBit(GPIOC, GPIO_Pin_9, (BitAction)(!GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_9)));
  115. IWDG_ReloadCounter();
  116. }
  117. }
  118. void WaitMs (uint16_t Ms)
  119. {
  120. TIM_SetCounter(TIM3, 0);
  121. if (Ms == 1)
  122. {
  123. TIM_PrescalerConfig(TIM3, (uint16_t)(24000000 / 10000), TIM_PSCReloadMode_Update);
  124. Ms = 10;
  125. }
  126. else
  127. {
  128. TIM_PrescalerConfig(TIM3, (uint16_t)(24000000 / 1000), TIM_PSCReloadMode_Update);
  129. }
  130. TIM_SetAutoreload(TIM3, Ms - 1);
  131. TIM_Cmd(TIM3, ENABLE); // Enables or disables the specified TIM peripheral.
  132. while (!(TIM_GetFlagStatus(TIM3, TIM_FLAG_Update)));
  133. TIM_ClearFlag(TIM3, TIM_FLAG_Update);
  134. TIM_Cmd(TIM3, DISABLE); // Enables or disables the specified TIM peripheral.
  135. }
  136. /**
  137. * @brief Returns 1-d table lookup result
  138. * @param Inp: lookuped value
  139. * @param In: Array
  140. * @param ArrSize: Array 2-nd dimension size
  141. * @retval Result value
  142. */
  143. uint32_t TableLookup1d(uint32_t Inp, uint32_t In[][20], uint32_t ArrSize)
  144. {
  145. uint32_t Outp = 0;
  146. int i = 0;
  147. /* ??????? ?? ????? */
  148. if (Inp >= In[0][ArrSize-1]) Outp = In[1][ArrSize-1];
  149. else if (Inp <= In[0][0]) Outp = In[1][0];
  150. /* */
  151. else
  152. {
  153. for (i = 1; i < ArrSize; i++)
  154. {
  155. /* ????? ??????? ??????? */
  156. if (Inp <= In[0][i])
  157. {
  158. /* val_x = val[n-1] + (val[n] - val[n-1]) * (ind_x - ind[n-1]) / (ind[n] - ind[n-1]) */
  159. Outp = In[1][i-1] + (In[1][i] - In[1][i-1]) * (Inp - In[0][i-1]) / (In[0][i] - In[0][i-1]);
  160. break;
  161. }
  162. }
  163. }
  164. return Outp;
  165. }
  166. // // ????? ?????? ????????
  167. // unsigned long mills_poll_REVL=0,
  168. // mills_poll_AUTO_LIGHT=0,
  169. // mills_poll_AMP=AMP_DELAY,
  170. // mills_poll_SAVG=SAVG_DELAY;
  171. // // ?????
  172. //
  173. // void setup(){
  174. // // ??? usart Serial.begin(115200);
  175. // // ?????? ????????? ??????? ????? revl_state=EEPROM.read(REVL_STATE_OFFSET);
  176. // // ?????? ??????? ???????????? sens = analogRead(LIGHT_SENS)>>2; // ?????????? ????????? ???????? ???????????? ??????? ????????????
  177. // }
  178. // void loop(){
  179. // // ???? ??????? ???????????? ?????? ????????? ????????
  180. // POLL(SAVG);
  181. // POLL(AUTO_LIGHT);
  182. // //POLL(AMP);
  183. // POLL(REVL);
  184. // }
  185. // void poll_SAVG(){ // ??????? ???????? ??????? ????????????
  186. // int sens_ = analogRead(LIGHT_SENS)>>2; //????????? ???????? ????????? ???????
  187. // if (sens_ > sens) { // ???? ??????? ?????? ???????? - ??????? ??????? ????? ?? ???? ???????
  188. // sens = sens + 1;
  189. // }
  190. // if (sens_ < sens) { // ???? ??????? ?????? ???????? - ??????? ??????? ???? ?? ???? ???????
  191. // sens = sens - 1;
  192. // }
  193. // //Serial.println(">>>*****");
  194. // Serial.print(" sens_=");
  195. // Serial.print(sens_, DEC);
  196. // //Serial.print(" sens=");
  197. // Serial.print(" / ");
  198. // Serial.println(sens, DEC);
  199. // Serial.println("*****");
  200. // p_delay(SAVG,SAVG_DELAY); // ???????? ??? ?????????? ??????.
  201. // }
  202. // //--------------------------------
  203. // // ????? ?????????????? ???????????? ?????
  204. // // ???????? ?????? ???? ? ??. CHIP-? (all-in-one project)
  205. // // ?????????: ????????? ? ?????????? 30 ???????? ?? ????? ?????? ?? 5 ????? (??????? ?? 32)
  206. // // ?????? ????????? ?????????? ??? ????????????? ??????? ? ??????
  207. // // ????=(????>>4 + ????_???? - ????_????_?????/2)
  208. // // ??? ???????? ????????? ????????? ??????? ? ????? ?? ??????? ? ?????? ?? ??????
  209. // // ?????, ??? ????? ??????. ???? ?? ??????????? ?????? ???????? ????? ?? ????????? CHIP
  210. // // \??????| 0 32 64 128 160 192 224
  211. // // ???? \ |-----------------------------------------------------
  212. // // 0 | 2 2 2 64 96 128 160
  213. // // 1 | 2 2 64 96 128 160 192
  214. // // 2 | 2 2 64 96 128 160 192
  215. // // 3 | 2 64 96 128 160 192 255
  216. // // 4 | 2 64 96 128 160 192 255
  217. // // 5 | 64 96 128 160 192 255 255
  218. // // 6 | 64 96 128 160 192 255 255
  219. // void poll_AUTO_LIGHT(){ // ?????????? ????????
  220. // int light;
  221. // byte light_grandis;
  222. // // byte last_light;
  223. // // int AUTO_LIGHT_DELAY = 3000;
  224. // // int sens = analogRead(LIGHT_SENS)>>2; // /4 :) // ?????????????
  225. // //int sens = analogRead(LIGHT_SENS);
  226. // // sens=((sens>>4)+ulk-MAX_ULK/2)>>1; // ????????? ????????????
  227. // // light=sens<=0?2:(sens>=6?255:(sens+1)<<5);
  228. // //int sens_grandis = analogRead(LIGHT_SENS_GRANDIS)>>2;
  229. // // int light_sw = analogRead(LIGHT_SWITCH)>>2;
  230. // //Serial.print(" sens=");
  231. // //Serial.println(sens, DEC);
  232. // //Serial.print(" sens_sred=");
  233. // //Serial.print(sens_sred, DEC);
  234. // //Serial.print(" insred=");
  235. // //Serial.print(InSrednee, DEC);
  236. // //Serial.print(" sensgrandis=");
  237. // //Serial.print(sens_grandis, DEC);
  238. //
  239. // // sens=((sens>>4)+ulk-MAX_ULK/2)>>1; // ????????? ????????????
  240. // // light=sens<=0?2:(sens>=6?255:(sens+1)<<5);
  241. // //??????????? ?? ??????? ????????????------------
  242. // //if ((sens > 0) && (sens<=917)) {light = 1;}
  243. // //if ((sens > 917) && (sens<=918)) {light = 5;}
  244. // //if ((sens > 918) && (sens<=920)) {light = 10;}
  245. // //if ((sens > 920) && (sens<=970)) {light = 15;}
  246. // //if ((sens > 970) && (sens<=975)) {light = 80;}
  247. // //if ((sens > 975) && (sens<=980)) {light = 100;}
  248. // //if ((sens > 980) && (sens<=990)) {light = 130;}
  249. // //if ((sens > 990) && (sens<=1000)) {light = 160;}
  250. // //if ((sens > 1000) && (sens<=1010)) {light = 200;}
  251. // //if ((sens > 1010) && (sens<=1020)) {light = 230;}
  252. // //if (sens > 1020) {light = 255;}
  253. // //-------------------
  254. // if ((sens > 0) && (sens<=5)) {light = 1;}
  255. // if ((sens > 5) && (sens<=10)) {light = 2;}
  256. // if ((sens > 10) && (sens<=15)) {light = 3;}
  257. // if ((sens > 15) && (sens<=20)) {light = 4;}
  258. // if ((sens > 20) && (sens<=30)) {light = 5;}
  259. // if ((sens > 30) && (sens<=40)) {light = 6;}
  260. // if ((sens > 40) && (sens<=50)) {light = 7;}
  261. // if ((sens > 50) && (sens<=60)) {light = 8;}
  262. // if ((sens > 60) && (sens<=80)) {light = 9;}
  263. // if ((sens > 80) && (sens<=100)) {light = 10;}
  264. // if ((sens > 100) && (sens<=120)) {light = 15;}
  265. // if ((sens > 120) && (sens<=140)) {light = 20;}
  266. // if ((sens > 140) && (sens<=170)) {light = 30;}
  267. // if ((sens > 170) && (sens<=190)) {light = 40;}
  268. // if ((sens > 190) && (sens<=200)) {light = 50;}
  269. // if ((sens > 200) && (sens<=210)) {light = 70;}
  270. // if ((sens > 210) && (sens<=215)) {light = 90;}
  271. // if ((sens > 215) && (sens<=220)) {light = 100;}
  272. // if ((sens > 220) && (sens<=225)) {light = 200;}
  273. // //if ((sens > 225) && (sens<=230)) {light = 150;}
  274. // //if ((sens > 230) && (sens<=232)) {light = 200;}
  275. // if (sens > 225) {light = 255;}
  276. // // ??????????? ?? ???????? ??????????
  277. // //if ((sens_grandis > 10) && (sens_grandis<100)) {light_grandis = 255;}
  278. // //if ((sens_grandis > 100) && (sens_grandis<150)) {light_grandis = 150;}
  279. // //if ((sens_grandis > 150) && (sens_grandis<200)) {light_grandis = 50;}
  280. // //if ((sens_grandis > 200) && (sens_grandis<254)) {light_grandis = 10;}
  281. // //-----------------------
  282. // // ????????? ???????? ?? ?????????
  283. // // if (analogRead(LIGHT_SWITCH) < 1000) {
  284. // // light = light_grandis;
  285. // // AUTO_LIGHT_DELAY = 1000;
  286. // // }
  287. // //Serial.print(" sw=");
  288. // //Serial.print(analogRead(LIGHT_SWITCH), DEC);
  289. // //Serial.print(" del=");
  290. // //Serial.print(AUTO_LIGHT_DELAY, DEC);
  291. //
  292. // //Serial.print(" light=");
  293. // //Serial.print(light, DEC);
  294. // //if (light!=last_light) {
  295. // // analogWrite(LIGHT_PIN, light);
  296. // // last_light=light;
  297. // // }
  298. // //int fadeValue;
  299. // //fadeValue = last_light;
  300. // //Serial.print(" last_light=");
  301. // //Serial.println(last_light, DEC);
  302. // Serial.print(" light=");
  303. // Serial.println(light, DEC);
  304. // //light = light * 100;
  305. // //last_light = last_light * 100;
  306. // if (light > last_light) {
  307. //
  308. // // fade in from min to max in increments of 1 points:
  309. // for(int fadeValue = last_light; fadeValue <= light; fadeValue +=1) {
  310. // // sets the value (range from 0 to 255):
  311. // // fadeValue=fadeValue / 100;
  312. // analogWrite(LIGHT_PIN, fadeValue);
  313. // // wait for 30 milliseconds to see the dimming effect
  314. // delay(30);
  315. // }
  316. // }
  317. //
  318. // if (light < last_light) {
  319. //
  320. // // fade in from max to min in increments of 1 points:
  321. // for(int fadeValue = last_light; fadeValue >= light; fadeValue -=1) {
  322. // // sets the value (range from 0 to 255):
  323. // analogWrite(LIGHT_PIN, fadeValue);
  324. // // wait for 30 milliseconds to see the dimming effect
  325. // delay(30);
  326. // }
  327. // }
  328. //
  329. // last_light=light;
  330. // //Serial.print(" last_light=");
  331. // //Serial.print(last_light, DEC);
  332. //
  333. // p_delay(AUTO_LIGHT,AUTO_LIGHT_DELAY); // ???????? ??? ?????????? ??????.
  334. // }
  335. // //--------------------------------
  336. // // ????????? ???????????? ?????? ?? ?????? ??????? ????
  337. // // ?????? ?? ????????? ????????? ? ???????? ???????,
  338. // // ??????? ???????? ???????? ?? ???????????? 1 ??? ? REVL_DELAY ??
  339. // void poll_REVL(){
  340. // byte temp;
  341. // temp=revl_state;
  342. // if (digitalRead(REVL_PIN) != HIGH){
  343. // switch (revl_state) {
  344. // case 0:
  345. // revl_state=1;
  346. // break;
  347. // case 1:
  348. // revl_state=2;
  349. // digitalWrite(AVSW_PIN, HIGH); // ?????? ?????? ?? ????????? ??????
  350. // }
  351. // } else {
  352. // if (revl_state==2) {
  353. // delay(REVL_DELAY_OFF);
  354. // digitalWrite(AVSW_PIN, LOW); // ????? ??????
  355. // }
  356. //
  357. // revl_state=0;
  358. //
  359. // }
  360. // p_delay(REVL,REVL_DELAY); // ???????? ??? ?????????? ??????.
  361. // if (temp!=revl_state){
  362. // EEPROM.write(REVL_STATE_OFFSET,revl_state);
  363. // }
  364. // }