Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. #define RCC_AHBENR (*(unsigned int *)(0x40021000 + 0x14))
  2. #define GPIOB_MODER (*(unsigned int *)(0x48000400 + 0x00))
  3. #define GPIOB_ODR (*(unsigned int *)(0x48000400 + 0x14))
  4. #define RCC_APB1ENR (*(unsigned int *)(0x40021000 + 0x1C))
  5. #define PWR_CR (*(unsigned int *)(0x40007000 + 0x00))
  6. #define RCC_BDCR (*(unsigned int *)(0x40021000 + 0x20))
  7. #define RTC_WPR (*(unsigned int *)(0x40002800 + 0x24))
  8. #define RTC_ISR (*(unsigned int *)(0x40002800 + 0x0C))
  9. #define RTC_DR (*(unsigned int *)(0x40002800 + 0x04))
  10. #define RTC_TR (*(unsigned int *)(0x40002800 + 0x00))
  11.  
  12. #define GPIOA_PUPDR (*(unsigned int *)(0x48000000 + 0x0C))
  13. #define GPIOA_IDR (*(unsigned int *)(0x48000000 + 0x10))
  14. #define GPIOA_ODR (*(unsigned int *)(0x48000000 + 0x14))
  15. #define GPIOA_MODER (*(unsigned int *)(0x48000000 + 0x00))
  16.  
  17. int test = 0;
  18. int token = 0;
  19. int counter = 0;
  20. int i;
  21.  
  22. void clockSetup() {
  23. RCC_APB1ENR = (1 << 28);
  24. PWR_CR = (1 << 8);
  25. RCC_BDCR = 0x8101;
  26. RTC_WPR = 0xCA;
  27. RTC_WPR = 0x53;
  28. RTC_ISR = (1 << 7);
  29. while ((RTC_ISR & 0x40) == 0) {
  30. asm("nop");
  31. }
  32. RTC_DR = 0x0;
  33. RTC_TR = 0x0;
  34. RTC_ISR &= (0 << 7);
  35. RTC_WPR = 0xFE;
  36. RTC_WPR = 0x64;
  37. PWR_CR &= (0 << 8);
  38. }
  39.  
  40. void boardSetup() {
  41. RCC_AHBENR |= (1 << 17);
  42. GPIOA_PUPDR |= 0x4;
  43. GPIOA_MODER |= 0x400;
  44. GPIOA_MODER |= 0x4000;
  45. }
  46.  
  47. void clock(int pulse) {
  48. for (int k = 0; k < pulse; k++) {
  49. GPIOA_ODR |= 0x20;
  50. GPIOA_ODR &= ~0x20;
  51. }
  52. }
  53.  
  54. void begin() {
  55. GPIOA_ODR |= 0x00A0;
  56. }
  57.  
  58. void start() {
  59. GPIOA_ODR &= ~0x80;
  60. GPIOA_ODR &= ~0x20;
  61. }
  62.  
  63. void stop() {
  64. GPIOA_ODR |= 0x20;
  65. GPIOA_ODR |= 0x80;
  66. }
  67.  
  68. void acknowledge() {
  69. clock(1);
  70. }
  71.  
  72. void zero() {
  73. GPIOA_ODR |= 0x80;
  74. clock(6);
  75. GPIOA_ODR &= ~0x80;
  76. clock(1);
  77. GPIOA_ODR |= 0x80;
  78. clock(1);
  79. GPIOA_ODR &= ~0x80;
  80. acknowledge();
  81. }
  82.  
  83. void one() {
  84. clock(1);
  85. GPIOA_ODR |= 0x80;
  86. clock(2);
  87. GPIOA_ODR &= ~0x80;
  88. clock(4);
  89. GPIOA_ODR |= 0x80;
  90. clock(1);
  91. GPIOA_ODR &= ~0x80;
  92. acknowledge();
  93. }
  94.  
  95. void two() {
  96. GPIOA_ODR |= 0x80;
  97. clock(2);
  98. GPIOA_ODR &= ~0x80;
  99. clock(1);
  100. GPIOA_ODR |= 0x80;
  101. clock(2);
  102. GPIOA_ODR &= ~0x80;
  103. clock(1);
  104. GPIOA_ODR |= 0x80;
  105. clock(2);
  106. GPIOA_ODR &= ~0x80;
  107. acknowledge();
  108. }
  109.  
  110. void three() {
  111. GPIOA_ODR |= 0x80;
  112. clock(4);
  113. GPIOA_ODR &= ~0x80;
  114. clock(2);
  115. GPIOA_ODR |= 0x80;
  116. clock(2);
  117. GPIOA_ODR &= ~0x80;
  118. acknowledge();
  119. }
  120.  
  121. void four() {
  122. clock(1);
  123. GPIOA_ODR |= 0x80;
  124. clock(2);
  125. GPIOA_ODR &= ~0x80;
  126. clock(2);
  127. GPIOA_ODR |= 0x80;
  128. clock(3);
  129. GPIOA_ODR &= ~0x80;
  130. acknowledge();
  131.  
  132. }
  133.  
  134. void five() {
  135. GPIOA_ODR |= 0x80;
  136. clock(1);
  137. GPIOA_ODR &= ~0x80;
  138. clock(1);
  139. GPIOA_ODR |= 0x80;
  140. clock(2);
  141. GPIOA_ODR &= ~0x80;
  142. clock(1);
  143. GPIOA_ODR |= 0x80;
  144. clock(3);
  145. GPIOA_ODR &= ~0x80;
  146. acknowledge();
  147. }
  148.  
  149. void six() {
  150. GPIOA_ODR |= 0x80;
  151. clock(1);
  152. GPIOA_ODR &= ~0x80;
  153. clock(1);
  154. GPIOA_ODR |= 0x80;
  155. clock(6);
  156. GPIOA_ODR &= ~0x80;
  157. acknowledge();
  158. }
  159.  
  160. void seven() {
  161. GPIOA_ODR |= 0x80;
  162. clock(3);
  163. GPIOA_ODR &= ~0x80;
  164. clock(4);
  165. GPIOA_ODR |= 0x80;
  166. clock(1);
  167. GPIOA_ODR &= ~0x80;
  168. acknowledge();
  169. }
  170.  
  171. void eight() {
  172. GPIOA_ODR |= 0x80;
  173. clock(8);
  174. GPIOA_ODR &= ~0x80;
  175. acknowledge();
  176. }
  177.  
  178. void nine() {
  179. GPIOA_ODR |= 0x80;
  180. clock(4);
  181. GPIOA_ODR &= ~0x80;
  182. clock(1);
  183. GPIOA_ODR |= 0x80;
  184. clock(3);
  185. GPIOA_ODR &= ~0x80;
  186. acknowledge();
  187.  
  188. }
  189. void dataCommand() {
  190. start();
  191. clock(6);
  192. GPIOA_ODR |= 0x80;
  193. clock(1);
  194. GPIOA_ODR &= ~0x80;
  195. clock(1);
  196. acknowledge();
  197. stop();
  198. }
  199.  
  200. void displayControllCommand() {
  201. start();
  202. clock(2);
  203. GPIOA_ODR |= 0x0080;
  204. clock(2);
  205. GPIOA_ODR &= ~0x0080;
  206. clock(3);
  207. GPIOA_ODR |= 0x0080;
  208. clock(1);
  209. GPIOA_ODR &= ~0x0080;
  210. acknowledge();
  211. stop();
  212. }
  213.  
  214. void addressCommand() {
  215. start();
  216. clock(6);
  217. GPIOA_ODR |= 0x80;
  218. clock(2);
  219. GPIOA_ODR &= ~0x0080;
  220. acknowledge();
  221. }
  222. void display(int sec, int tenSec, int min, int tenMin) {
  223. addressCommand();
  224. switch (tenMin) {
  225. case 0:
  226. zero();
  227. break;
  228. case 1:
  229. one();
  230. break;
  231. case 2:
  232. two();
  233. break;
  234. case 3:
  235. three();
  236. break;
  237. case 4:
  238. four();
  239. break;
  240. case 5:
  241. five();
  242. break;
  243. case 6:
  244. six();
  245. break;
  246. case 7:
  247. seven();
  248. break;
  249. case 8:
  250. eight();
  251. break;
  252. case 9:
  253. nine();
  254. break;
  255. }
  256.  
  257. switch (min) {
  258. case 0:
  259. zero();
  260. break;
  261. case 1:
  262. one();
  263. break;
  264. case 2:
  265. two();
  266. break;
  267. case 3:
  268. three();
  269. break;
  270. case 4:
  271. four();
  272. break;
  273. case 5:
  274. five();
  275. break;
  276. case 6:
  277. six();
  278. break;
  279. case 7:
  280. seven();
  281. break;
  282. case 8:
  283. eight();
  284. break;
  285. case 9:
  286. nine();
  287. break;
  288. }
  289.  
  290. switch (tenSec) {
  291. case 0:
  292. zero();
  293. break;
  294. case 1:
  295. one();
  296. break;
  297. case 2:
  298. two();
  299. break;
  300. case 3:
  301. three();
  302. break;
  303. case 4:
  304. four();
  305. break;
  306. case 5:
  307. five();
  308. break;
  309. case 6:
  310. six();
  311. break;
  312. case 7:
  313. seven();
  314. break;
  315. case 8:
  316. eight();
  317. break;
  318. case 9:
  319. nine();
  320. break;
  321. }
  322.  
  323. switch (sec) {
  324. case 0:
  325. zero();
  326. break;
  327. case 1:
  328. one();
  329. break;
  330. case 2:
  331. two();
  332. break;
  333. case 3:
  334. three();
  335. break;
  336. case 4:
  337. four();
  338. break;
  339. case 5:
  340. five();
  341. break;
  342. case 6:
  343. six();
  344. break;
  345. case 7:
  346. seven();
  347. break;
  348. case 8:
  349. eight();
  350. break;
  351. case 9:
  352. nine();
  353. break;
  354. }
  355.  
  356. stop();
  357. }
  358.  
  359. int timerDelay(int sec) {
  360. if ((RTC_TR & 0x1) == 0) {
  361. if (test == 0) {
  362. sec++;
  363. test++;
  364. }
  365.  
  366. }
  367. if ((RTC_TR & 0x1) == 1) {
  368. if (test == 1) {
  369. sec++;
  370. test--;
  371. }
  372.  
  373. }
  374. return sec;
  375. }
  376.  
  377. void reset(int sec, int tenSec, int min, int tenMin) {
  378. sec = 0;
  379. tenSec = 0;
  380. min = 0;
  381. tenMin = 0;
  382. display(sec%10, sec/10%6, sec/60%10, sec/600%6);
  383. }
  384.  
  385. int main() {
  386. int sec;
  387.  
  388. boardSetup();
  389. begin();
  390. dataCommand();
  391. displayControllCommand();
  392. clockSetup();
  393. reset(sec%10, sec/10%6, sec/60%10, sec/600%6);
  394. token = ~token;
  395.  
  396. while (1) {
  397. display(sec%10, sec/10%6, sec/60%10, sec/600%6);
  398. sec = timerDelay(sec);
  399. }
  400. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement