Controller area networks (CAN) are widely used in the development of embedded real-time systems. As embedded systems become more complex, the development of dependable software for such systems is becoming a challenging problem. In this paper, we propose a technique to develop dependable synchronization code for CAN-based embedded systems. Our approach is to factor out synchronization as a separate aspect, synthesize synchronization code and then compose it with the functional code. Specifically, we allow the designer of a CAN-based application to first design the core functional code. The designer can then annotate the functional code with control points and specify high-level "global invariants" specifying the synchronization policies. Our methodology generates synchronization code based on message passing in a CAN system from the invariant and then automatically integrates the synchronization code into the functional code at appropriate control points. We propose and evaluate two solutions: one of which is based on a centralized active monitor and the other is a decentralized solution. The synchronization code developed is derived from high-level formal specifications via formal methods and is correct-by-construction, and will guarantee high assurance in safety-critical applications.